class SuperuserGenerator
Attributes
attributes[RW]
Public Instance Methods
generate_controller()
click to toggle source
NOTE: the order of the following methods is important!
# File lib/generators/superuser/superuser_generator.rb, line 9 def generate_controller template "controller_template.rb", "app/controllers/superuser/#{naming(:resources)}_controller.rb" end
generate_route_and_link()
click to toggle source
# File lib/generators/superuser/superuser_generator.rb, line 13 def generate_route_and_link # add resources to route if not exists route_replacement = "resources :#{resources}" r = add_resources_route 'config/routes.rb', search = 'namespace :superuser do', route_replacement # add link to resources in the layout if not exists link = "<%= link_to '#{resources}', [:superuser, :#{resources}] %>" add_layout_links 'app/views/layouts/superuser/application.html.erb', search = '<div class="sidebar_item">', link end
generate_views()
click to toggle source
# File lib/generators/superuser/superuser_generator.rb, line 23 def generate_views template "views/_form.html.erb", "app/views/superuser/#{naming(:resources)}/_form.html.erb" template "views/index.html.erb", "app/views/superuser/#{naming(:resources)}/index.html.erb" template "views/show.html.erb", "app/views/superuser/#{naming(:resources)}/show.html.erb" template "views/new.html.erb", "app/views/superuser/#{naming(:resources)}/new.html.erb" template "views/edit.html.erb", "app/views/superuser/#{naming(:resources)}/edit.html.erb" end
Private Instance Methods
add_layout_links(relative_file, search_text, replace_text)
click to toggle source
# File lib/generators/superuser/superuser_generator.rb, line 126 def add_layout_links(relative_file, search_text, replace_text) path = destination_path(relative_file) file_content = File.read(path) unless file_content.include? replace_text content = file_content.sub(/(#{Regexp.escape(search_text)})/mi, "#{search_text}\n\t\t\t\t#{replace_text}") File.open(path, 'wb') { |file| file.write(content) } end end
add_resources_route(relative_file, search_text, replace_text)
click to toggle source
sub_file modified
# File lib/generators/superuser/superuser_generator.rb, line 103 def add_resources_route(relative_file, search_text, replace_text) path = destination_path(relative_file) file_content = File.read(path) # if namespace for :superuser don't exists then create it unless file_content.include? 'namespace :superuser do' route "\tnamespace :superuser do\n\t\tresources :#{resources}\n\tend" return end # the regular expression string should be between single quotes not double quotes # the regular expression string should not include delimiters # the matching will stop when find the first occurence of 'end' regex_string = 'namespace \:superuser do[^end]*' + replace_text regex = Regexp.new(regex_string) #unless file_content.include? replace_text unless regex.match file_content content = file_content.sub(/(#{Regexp.escape(search_text)})/mi, "#{search_text}\n\t\t#{replace_text}") File.open(path, 'wb') { |file| file.write(content) } end end
destination_path(path)
click to toggle source
# File lib/generators/superuser/superuser_generator.rb, line 98 def destination_path(path) File.join(destination_root, path) end
editable_attributes()
click to toggle source
# File lib/generators/superuser/superuser_generator.rb, line 68 def editable_attributes attributes ||= model_columns_for_attributes.map do |column| {name: column.name.to_s, type: column.type.to_s} end end
field_type(db_type)
click to toggle source
# File lib/generators/superuser/superuser_generator.rb, line 84 def field_type(db_type) matching_type = { decimal: "text_field", float: "text_field", datetime: "text_field", string: "text_field", integer: "text_field", text: "text_area", json: "text_area", jsonb: "text_area" } matching_type[db_type.to_sym] || "text_field" end
get_controller_name()
click to toggle source
# File lib/generators/superuser/superuser_generator.rb, line 48 def get_controller_name resources_name.camelize end
get_model()
click to toggle source
# File lib/generators/superuser/superuser_generator.rb, line 74 def get_model resources_name.classify.constantize end
get_resource_attributes()
click to toggle source
# File lib/generators/superuser/superuser_generator.rb, line 78 def get_resource_attributes editable_attributes.map { |a| a.name.prepend(':') }.join(', ') end
model_columns_for_attributes()
click to toggle source
# File lib/generators/superuser/superuser_generator.rb, line 62 def model_columns_for_attributes resources_name.classify.constantize.columns.reject do |column| column.name.to_s =~ /^(id|user_id|created_at|updated_at)$/ end end
naming(key)
click to toggle source
# File lib/generators/superuser/superuser_generator.rb, line 52 def naming(key) map = { resources: resources_name.underscore, resource: resources_name.singularize.underscore, controller_name: resources_name.camelize, model_name: resources_name.classify } return map[key] end
replace(file_path)
click to toggle source
# File lib/generators/superuser/superuser_generator.rb, line 33 def replace(file_path) gsub_file file_path, 'resources', "#{naming(:resources)}" gsub_file file_path, 'resource', "#{naming(:resource)}" gsub_file file_path, 'ControllerName', "#{naming(:controller_name)}" gsub_file file_path, 'ModelName', "#{naming(:model_name)}" end
resource()
click to toggle source
# File lib/generators/superuser/superuser_generator.rb, line 44 def resource resources_name.singularize.underscore end
resources()
click to toggle source
# File lib/generators/superuser/superuser_generator.rb, line 40 def resources resources_name.underscore end