lib/rails/generators/humdrum/resource/resource_generator.rb in humdrum-rails-0.0.7 vs lib/rails/generators/humdrum/resource/resource_generator.rb in humdrum-rails-0.0.8
- old
+ new
@@ -16,14 +16,14 @@
desc "Generates a controller, view and route entries for CRUD operations of a resource"
argument :resource_name, :type=>:string
argument :fields, :type=>:hash, :banner =>"Resource Fields."
- class_option :fluid, :type => :boolean, :default => true, :desc => "Pass true to create fluid layouts"
+ class_option :fixed, :type => :boolean, :default => false, :desc => "Create fixed layouts. Default is fluid (Gumby doesn't support fluid framework. Hence applicable only for bootstrap for the timebeing.)"
class_option :debug, :type => :boolean, :default => false, :desc => "This will print the arguments for debugging"
- class_option :front_end_framework, :type => :string, :default => 'gumby', :desc => "Support Twitter Bootstrap (twitter.github.io/bootstrap/) and Gumpy (http://gumbyframework.com/). Default is bootstrap. Pass gumby for Gumby Framework"
+ class_option :front_end_framework, :type => :string, :default => 'bootstrap', :desc => "Support Twitter Bootstrap (twitter.github.io/bootstrap/) and Gumpy (http://gumbyframework.com/). Default is bootstrap. Pass gumby for Gumby Framework"
def debug_args
print_args if options.debug?
end
@@ -32,10 +32,11 @@
end
def generate_views
template "views/#{options.front_end_framework}/resource/_edit.html.erb", "app/views/#{controller_path}/_edit.html.erb"
template "views/#{options.front_end_framework}/resource/_filters.html.erb", "app/views/#{controller_path}/_filters.html.erb"
+ template "views/#{options.front_end_framework}/resource/_field.html.erb", "app/views/#{controller_path}/_field.html.erb"
template "views/#{options.front_end_framework}/resource/_form.html.erb", "app/views/#{controller_path}/_form.html.erb"
template "views/#{options.front_end_framework}/resource/_index.html.erb", "app/views/#{controller_path}/_index.html.erb"
template "views/#{options.front_end_framework}/resource/_item.html.erb", "app/views/#{controller_path}/_item.html.erb"
template "views/#{options.front_end_framework}/resource/_nav_filters.html.erb", "app/views/#{controller_path}/_nav_filters.html.erb"
template "views/#{options.front_end_framework}/resource/_new.html.erb", "app/views/#{controller_path}/_new.html.erb"
@@ -58,25 +59,28 @@
def generate_javascript_validations
template "javascripts/validations/validator.js", "app/assets/javascripts/validations/#{model_path}.js"
end
def generate_migrations
- migration_template "migrations/create_resources.rb", "db/migrate/create_#{instances_name}"
+ 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 "migrations/create_resources.rb", "db/migrate/create_#{instances_name}.rb"
+ end
end
def generate_routes
words = name_phrases
resource = words.pop
route words.reverse.inject("resources :#{resource.pluralize}") { |acc, phrase|
"namespace(:#{phrase}){ #{acc} }"
}
end
- def generate_locales
- template "config/locales/humdrum.en.yml", "config/locales/humdrum.en.yml"
- end
-
private
def print_args
puts ":fields: #{fields}"
puts ":form_link_param: #{form_link_param}"
@@ -150,10 +154,18 @@
def instances_name
instance_name.pluralize
end
+ def instance_title
+ instance_name.titleize
+ end
+
+ def instances_title
+ instances_name.titleize
+ end
+
def table_name
instances_name
end
def resource_link(actn='index', ltype='path')
@@ -185,37 +197,10 @@
else
"@#{resource.downcase}"
end
end
- def input_type(name, type)
- if name.include?("email") && type == "string"
- return "email"
- elsif name.include?("password") && type == "string"
- return "password"
- elsif (name.include?("phone") || name.include?("mobile")) && type == "string"
- return "tel"
- else
- case type
- when "string"
- "text"
- when "text"
- "textarea"
- when "integer"
- "number"
- when "references"
- "type"
- when "date"
- "date"
- when "datetime"
- "datetime-local"
- when "timestamp", "time"
- "time"
- end
- end
- end
-
## List of all the string fields
def string_fields
main_field = main_string_field
fields.map{|name, type| name if name != main_field && type == "string" }.uniq.compact
end
@@ -229,31 +214,68 @@
## The main string field like 'name'
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
+ 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
+
## Text Fields like description or summary
def text_fields
fields.map{|name, type| name if type == "text"}.uniq.compact
end
def container_class
if options.front_end_framework == "bootstrap"
- options.fluid? ? "container-fluid" : "container"
+ options.fixed? ? "container" : "container-fluid"
elsif options.front_end_framework == "gumby"
"container"
else
- "container"
+ options.fixed? ? "container" : "container-fluid"
end
end
def row_class
if options.front_end_framework == "bootstrap"
- options.fluid? ? "row-fluid" : "row"
+ options.fixed? ? "row" : "row-fluid"
elsif options.front_end_framework == "gumby"
"row"
else
- "row"
+ options.fixed? ? "row" : "row-fluid"
end
end
def column_class(grid_width_value)
grid_width_value_hash = {"1" => "one",
\ No newline at end of file