Class: HotGlue::TypeaheadGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
DefaultConfigLoader
Defined in:
lib/generators/hot_glue/typeahead_generator.rb

Instance Method Summary collapse

Methods included from DefaultConfigLoader

#default_configs, #get_default_from_config

Constructor Details

#initialize(*meta_args) ⇒ TypeaheadGenerator

Returns a new instance of TypeaheadGenerator.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/generators/hot_glue/typeahead_generator.rb', line 20

def initialize(*meta_args)
  super
  begin
    @the_object = eval(meta_args[0][0])
  rescue StandardError => e
    message = "*** Oops: It looks like there is no object for #{meta_args[0][0]}. Please define the object + database table first."
    puts message
    raise(HotGlue::Error, message)
  end
  @pundit = get_default_from_config(key: :pundit_default)

  @singular = args.first.tableize.singularize
  @class_name = args.first
  @plural = args.first.tableize.pluralize
  @namespace = options['namespace']
  @nested = options['nested'] if options['nested']

  if !@nested.nil?
    @nested_set = @nested.split("/").collect { |arg|
      is_optional = arg.start_with?("~")
      arg.gsub!("~", "")
      {
        singular: arg,
        plural: arg.pluralize,
        optional: is_optional
      }
    }
    puts "NESTING: #{@nested_set}"
  else
    @nested_set = []
  end

  @auth = options['auth'] || "current_user"
  @auth_identifier = options['auth_identifier'] || "user"

  if options['search_by']
    @search_by = options['search_by'].split(",")
  else
    # todo: read the fields off the table

    eligible_columns = @the_object.columns.filter{ |column|
      column.sql_type == "character varying"
    }
    columns = eligible_columns.map(&:name).map(&:to_sym).reject { |field| [:id, :created_at, :updated_at].include?(field) }
    @search_by = columns.collect(&:to_s)

  end
  @meta_args = meta_args
  dest_file = "#{'spec/dummy/' if $INTERNAL_SPECS}app/controllers/#{@namespace ? @namespace + "/" : ""}#{@plural }_typeahead_controller.rb"
  template "typeahead_controller.rb.erb", dest_file


  dirname =  "app/views/#{@namespace ? @namespace + "/" : ""}#{@plural}_typeahead"

  if !Dir.exist?(dirname)
    Dir.mkdir dirname
  end
  {"typeahead_views/_thing.html.erb" => "_#{@singular}.html.erb",
   "typeahead_views/index.html.erb" => "index.html.erb"}.each do |source_filename, dest_filename|

    dest_filepath = File.join("#{'spec/dummy/' if $INTERNAL_SPECS}app/views#{namespace_with_dash}",
                              "#{@plural}_typeahead", dest_filename)

    template source_filename, dest_filepath
    text = File.read(dest_filepath)
    text.gsub!('<\\%=', '<%=' )
    text.gsub!('<\\%', '<%' )
    File.open(dest_filepath, "w") { |f| f.write text }
  end


  puts ""
  puts "be sure to add this your config/routes.rb file:"
  puts ""
  if @namespace
    puts "namespace :#{@namespace} do"
  end
  puts "  resources :#{@plural}_typeahead, only: [:index]"
  if @namespace
    puts "end"
  end
  puts ""

end

Instance Method Details

#auth_objectObject



106
107
108
# File 'lib/generators/hot_glue/typeahead_generator.rb', line 106

def auth_object
  "#{@auth}_id"
end

#controller_descends_fromObject



122
123
124
125
126
127
128
# File 'lib/generators/hot_glue/typeahead_generator.rb', line 122

def controller_descends_from
  if defined?(@namespace.titlecase.gsub(" ", "") + "::BaseController")
    @namespace.titlecase.gsub(" ", "") + "::BaseController"
  else
    "ApplicationController"
  end
end

#filepath_prefixObject



14
15
16
17
# File 'lib/generators/hot_glue/typeahead_generator.rb', line 14

def filepath_prefix
  # todo: inject the context
  'spec/dummy/' if $INTERNAL_SPECS
end

#namespace_with_dashObject



110
111
112
113
114
115
116
# File 'lib/generators/hot_glue/typeahead_generator.rb', line 110

def namespace_with_dash
  if @namespace
    "/#{@namespace}"
  else
    ""
  end
end

#regenerate_me_codeObject



130
131
132
# File 'lib/generators/hot_glue/typeahead_generator.rb', line 130

def regenerate_me_code
  "bin/rails generate hot_glue:typeahead #{ @meta_args[0][0] } #{@meta_args[1].collect { |x| x.gsub(/\s*=\s*([\S\s]+)/, '=\'\1\'') }.join(" ")}"
end