lib/generators/faalis/scaffold_generator.rb in faalis-1.0.0.alpha0 vs lib/generators/faalis/scaffold_generator.rb in faalis-1.0.0.alpha1

- old
+ new

@@ -21,28 +21,32 @@ module Generators # Generator Full Faalis scaffold with dashboard class ScaffoldGenerator < Rails::Generators::Base include Faalis::Generators::Concerns::RequireFields include Faalis::Generators::Concerns::Parent + include Faalis::Generators::Concerns::Child include Faalis::Generators::Concerns::JsonInput include Faalis::Generators::Concerns::ResourceName include Faalis::Generators::Concerns::ResourceFields + include Faalis::Generators::Concerns::Globalize + desc 'Create full faalis full scaffold' - # FIXME: These options should have :type and :desc or even default + # FIXME: These options should have :desc or even default # value - class_option :no_model - class_option :no_route - class_option :no_controller - class_option :no_migration - class_option :no_asset + class_option :no_model, :type => :boolean + class_option :no_route, :type => :boolean + class_option :no_controller, :type => :boolean + class_option :no_migration, :type => :boolean + class_option :no_asset, :type => :boolean - # FIXME: Add method documents HERE + # This method will create full scaffold based on user options def create_scaffold if options.empty? # TODO: .... end + # TODO: Show error if in class_option migration is selected by model not create_model unless options[:no_model] create_route unless options[:no_route] create_controller unless options[:no_controller] create_list_view end @@ -51,11 +55,10 @@ # Create a dedicated controller, # It does not have any relation to Faalis # TODO: Check for better way def create_controller - #invoke "scaffold_controller", resource_data["name"] if options[:no_asset] `rails g scaffold_controller #{resource_data["name"]} --skip` else `rails g scaffold_controller #{resource_data["name"]}` end @@ -70,70 +73,114 @@ #It does not support has_many relation yet def create_model result = [] all_fields = [] relations = "\n" - fields.each do |name, type, to| + fields.each do |name, type, to| case type when 'belongs_to' type_ = 'integer' if to.singularize != name - relations << "belongs_to :#{name.singularize}, + relations << " belongs_to :#{name.singularize}, :class_name => \"#{to.singularize.capitalize}\"\n" else - relations << "belongs_to :#{to.singularize}\n" + relations << " belongs_to :#{to.singularize}\n" end name_ = "#{name.singularize}_id" result << [name_, type_] - when 'text', 'integer', 'string', 'boolean', 'datetime' + when 'text', 'integer', 'string', 'boolean', 'datetime', 'date', 'float' result << [name, type] when 'image' generate "paperclicp #{resource_data['name']} #{name}" - relations << "has_attached_file :#{name}\n" - relations << "validates_attachment_content_type :image, :content_type => %w(image/jpeg image/jpg image/png),:less_than => 1.megabytes]\n" + relations << " has_attached_file :#{name}\n" + relations << " validates_attachment_content_type :#{name}, + content_type: %w(image/jpeg image/jpg image/png), + less_than: 1.megabytes]\n" + # TODO: Run this generator just once for all images + `rails generate paperclip #{resource_data['name']} #{name}` when 'tag' rake "rake acts_as_taggable_on_engine:install:migrations" - relations << "acts_as_taggable_on :#{name}\n" + relations << " acts_as_taggable_on :#{name}\n" result << [name, 'string'] when 'in' result << [name, 'string'] when 'has_many' - relations << "has_and_belongs_to_many :#{to}\n" - say_status 'warn', "There is a many to many relation between #{resource_data['name']} to #{to}, You should create it manually in model files" + relations << " has_and_belongs_to_many :#{to}\n" + say_status 'warn', "There is a many to many relation between #{resource_data['name']} and #{to}, + You should create it manually in model files" end + end - all_fields = result.collect do |x| - x.join(':') + if parent? + parents.each do |parent| + result << ["#{parent.singularize}_id", "integer"] + relations << " belongs_to :#{parent.singularize}\n" end + end + + childs.each do |child| + relations << " has_many :#{child.pluralize}\n" end - if parent? - all_fields << ["#{resource_data["parents"]}_id", "integer"] + all_fields = result.collect do |x| + x.join(':') end - invoke('active_record:model', [resource_data['name'], *all_fields], { + # Load all globalize field and create a string to adding in model + globalizes = "\n translates "+ + globalize_fields.map { |field | ":#{field['name'].underscore}" }.join(", ") + + + + invoke('active_record:model', [resource_data['name'].underscore, *all_fields], { migration: !options[:no_migration], timestamps: true }) + if globalize_fields.any? + create_globalize_migration + end + if File.exist?("app/models/#{resource_data["name"]}.rb") - inject_into_file "app/models/#{resource_data["name"]}.rb", after: 'Base' do - relations + + inject_into_file "app/models/#{resource_data["name"].underscore}.rb", after: 'Base' do + + globalize_fields.empty? ? relations : relations + globalizes end else - puts "Could not find file app/models/#{resource_data["name"]}" + puts "Could not find file app/models/#{resource_data["name"].underscore}" end + + + end #Invoke Faalis list view generator def create_list_view invoke 'faalis:js:list_view', [jsonfile] + end + + def create_globalize_migration + `rails g migration add_globalize_to_#{resource_data["name"].underscore} ` + Dir["#{Rails.root}/db/migrate/**/*globalize*.rb"].each {|file| require file } + klass = "add_globalize_to_#{resource_data['name']}".underscore.camelize + + migration_class = "::#{klass}".constantize + migration_path = migration_class.instance_method(:change).source_location + + fields = globalize_fields.map { |field | ":#{field['name']} => :#{field['type']}" }.join(", ") + + inject_into_file migration_path[0], after: 'change' do + "\n #{resource_data['name'].camelize}"+ + '.create_translation_table! '+ + fields + end end end end end