class Kuppayam::ResourceGenerator
require 'rails/generators' require 'rails/generators/migration'
Public Class Methods
next_migration_number(path)
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 8 def self.next_migration_number(path) @migration_number = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i.to_s end
Public Instance Methods
debug_args()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 21 def debug_args print_args if options.debug? end
generate_controller_spec()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 52 def generate_controller_spec template "spec/controllers/resource_controller_spec.rb", "spec/controllers/#{controller_path}_controller_spec.rb" end
generate_controllers()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 48 def generate_controllers template "controllers/resource_controller.rb", "app/controllers/#{controller_path}_controller.rb" end
generate_factory()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 40 def generate_factory template "spec/factories/resource.rb", "spec/factories/#{model_path}.rb" end
generate_migrations()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 25 def generate_migrations migration_dir = "db/migrate" migration_file_name = "create_#{instances_name}.rb" destination = self.class.migration_exists?(migration_dir, migration_file_name) if destination say_status("skipped", "Migration #{migration_file_name}.rb already exists") else migration_template "db/migrate/create_resources.rb", "db/migrate/create_#{instances_name}.rb" end end
generate_model()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 36 def generate_model template "models/resource.rb", "app/models/#{model_path}.rb" end
generate_model_spec()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 44 def generate_model_spec template "spec/models/resource_spec.rb", "spec/models/#{model_path}_spec.rb" end
generate_models()
click to toggle source
# File lib/generators/resource/resource_generator.rb, line 51 def generate_models template "models/resource.rb", "app/models/#{model_path}.rb" end
generate_views()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 56 def generate_views template "views/_form.html.erb", "app/views/#{controller_path}/_form.html.erb" template "views/_index.html.erb", "app/views/#{controller_path}/_index.html.erb" template "views/_row.html.erb", "app/views/#{controller_path}/_row.html.erb" template "views/_show.html.erb", "app/views/#{controller_path}/_show.html.erb" template "views/index.html.erb", "app/views/#{controller_path}/index.html.erb" end
Private Instance Methods
column_class(grid_width_value)
click to toggle source
# File lib/generators/resource/resource_generator.rb, line 267 def column_class(grid_width_value) grid_width_value_hash = {"1" => "one", "2" => "two", "3" => "three", "4" => "four", "5" => "five", "6" => "six", "7" => "seven", "8" => "eight", "9" => "nine", "10" => "ten", "11" => "eleven", "12" => "twelve", "13" => "thirteen", "14" => "fourteen", "15" => "fifteen", "16" => "sixteen"} case framework when "bootstrap3" "col-md-" when "bootstrap2" "span#{grid_width_value}" when "gumby" "#{grid_width_value_hash[grid_width_value.to_s]} columns" else "col#{grid_width_value}" end end
container_class()
click to toggle source
# File lib/generators/resource/resource_generator.rb, line 245 def container_class case framework when "bootstrap3", "gumby" "container" when "bootstrap2" options.fixed? ? "container" : "container-fluid" else "container" end end
controller_class()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 115 def controller_class words = name_phrases resource = words.pop if words.any? words.collect(&:camelize).join("::") + "::#{resource.camelize.pluralize}Controller" else "#{resource.camelize.pluralize}Controller" end end
controller_path()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 105 def controller_path words = name_phrases resource = words.pop if words.any? words.collect(&:downcase).join("/") + "/#{resource.pluralize}" else "#{resource.pluralize}" end end
form_for_object()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 172 def form_for_object if name_phrases.size > 1 words = name_phrases.dup resource = words.pop "[" + words.map{|x| ":#{x}"}.join(", ") + ", @#{resource}]" else "@#{instance_name}" end end
form_link_param()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 183 def form_link_param words = name_phrases resource = words.pop if words.any? # to print like this [:admin, :user, :location, @chakka] # in form.html.erb "[" + (words.map{|x| ":" + x.downcase} << "@" + resource.downcase).join(", ") + "]" else "@#{resource.downcase}" end end
guess_input_type(name, type)
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 212 def guess_input_type(name, type) case type when "string" if name.include?("url") return "url" elsif name.include?("email") return "email" elsif name.include?("phone") || name.include?("mobile") || name.include?("landline") || name.include?("contact number") return "tel" elsif name.include?("time") return "time" elsif name.include?("date") return "date" elsif name.include?("password") return "password" else "text" end when "text" "textarea" when "integer" "number" when "references" "type" when "date" "date" when "datetime" "datetime-local" when "timestamp", "time" "time" when "boolean" "checkbox" else "text" end end
instance_name()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 133 def instance_name name_phrases.last.underscore end
instance_title()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 141 def instance_title instance_name.titleize end
instances_name()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 137 def instances_name instance_name.pluralize end
instances_title()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 145 def instances_title instances_name.titleize end
main_string_field()
click to toggle source
The main string field like 'name'
# File lib/generators/kuppayam/resource_generator.rb, line 208 def main_string_field fields.map{|name, type| name if name.include?("name") && type == "string"}.uniq.compact || fields.keys.any? ? fields.keys.first : "id" end
model_class()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 129 def model_class name_phrases.last.camelize end
model_path()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 125 def model_path name_phrases.last.downcase end
name_phrases()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 95 def name_phrases if resource_name.include?('::') resource_name.split("::") elsif resource_name.include?('/') resource_name.split("/") else [resource_name] end end
print_args()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 66 def print_args puts ":fields: #{fields}" puts ":form_link_param: #{form_link_param}" puts "name_phrases: #{name_phrases}" puts "controller_path: #{controller_path}" puts "controller_class: #{controller_class}" puts "model_path: #{model_path}" puts "model_class: #{model_class}" puts "instance_name: #{instance_name}" puts "instances_name: #{instances_name}" puts "table_name: #{table_name}" puts "index path: #{resource_link}" puts "show path: #{resource_link('show')}" puts "new path: #{resource_link('new')}" puts "edit path: #{resource_link('edit')}" puts "create path: #{resource_link('create')}" puts "update path: #{resource_link('update')}" puts "destroy path: #{resource_link('destroy')}" puts "index url: #{resource_link('index','url')}" puts "show url: #{resource_link('show','url')}" puts "new url: #{resource_link('new','url')}" puts "edit url: #{resource_link('edit','url')}" puts "create url: #{resource_link('create','url')}" puts "update url: #{resource_link('update','url')}" puts "destroy url: #{resource_link('destroy','url')}" end
resource_link(actn='index', ltype='path')
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 153 def resource_link(actn='index', ltype='path') map = { 'index' => '', 'show' => '', 'edit' => 'edit_', 'new' => 'new_', 'update' => '', 'create' => '', 'destroy' => '', } words = name_phrases resource = words.pop if actn == "index" map[actn] + (words.any? ? words.join("_") + "_" : "") + resource.pluralize + "_" + ltype else map[actn] + (words.any? ? words.join("_") + "_" : "") + resource + "_" + ltype end end
row_class()
click to toggle source
# File lib/generators/resource/resource_generator.rb, line 256 def row_class case framework when "bootstrap3", "gumby" "row" when "bootstrap3" options.fixed? ? "row" : "row-fluid" else "row" end end
string_fields()
click to toggle source
List of all the string fields
# File lib/generators/kuppayam/resource_generator.rb, line 196 def string_fields main_field = main_string_field fields.map{|name, type| name if name != main_field && type == "string" }.uniq.compact end
string_fields_including_main_field()
click to toggle source
List of all the string fields including main field
# File lib/generators/kuppayam/resource_generator.rb, line 202 def string_fields_including_main_field main_field = main_string_field fields.map{|name, type| name if type == "string" }.uniq.compact end
table_name()
click to toggle source
# File lib/generators/kuppayam/resource_generator.rb, line 149 def table_name instances_name end
text_fields()
click to toggle source
Text Fields like description or summary
# File lib/generators/kuppayam/resource_generator.rb, line 250 def text_fields fields.map{|name, type| name if type == "text"}.uniq.compact end