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
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
|