Class: Mdwa::Generators::UserScaffoldGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- Mdwa::Generators::UserScaffoldGenerator
- Includes:
- Rails::Generators::Migration
- Defined in:
- lib/generators/mdwa/user_scaffold/user_scaffold_generator.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
Returns the value of attribute model.
-
#specific_model ⇒ Object
Returns the value of attribute specific_model.
Instance Method Summary collapse
- #controller_and_view ⇒ Object
-
#initialize(*args, &block) ⇒ UserScaffoldGenerator
constructor
A new instance of UserScaffoldGenerator.
- #migration_override ⇒ Object
- #model_override ⇒ Object
- #run_rake_db_migrate ⇒ Object
Constructor Details
#initialize(*args, &block) ⇒ UserScaffoldGenerator
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 |
# File 'lib/generators/mdwa/user_scaffold/user_scaffold_generator.rb', line 29 def initialize(*args, &block) super @model = MDWA::Generators::Model.new( scaffold_name ) # model_name is not valid print_usage unless @model.valid? # verifies specific model name @specific_model = MDWA::Generators::Model.new( .model ) unless .model.blank? if !@specific_model.nil? if !@specific_model.valid? print_usage else @model.specific_model_name = @specific_model.raw end end require_all "#{Rails.root}/app/models/user.rb" @predefined_fields = User.column_names @predefined_fields << 'password' @predefined_fields << 'password_confirmation' # sets the model attributes attributes.each do |attribute| attr_name = attribute.split(':').first if !User.accessible_attributes.to_a.include?( attr_name ) @model.add_attribute MDWA::Generators::ModelAttribute.new( attribute ) end end unless .only_diff_migration generate "mdwa:scaffold #{scaffold_name} name:string email:string password:password password_confirmation:password #{@model.attributes.collect{|a| a.raw}.join(' ')} #{'--force' if options.force} #{'--ajax' if options.ajax} #{"model=#{options.model}" if options.model} #{'--skip_interface' if options.skip_interface} #{'--only_interface' if options.only_interface} #{'--skip_rake_migrate' if options.skip_rake_migrate} #{'--skip_timestamp' if options.skip_timestamp} #{'--skip_questions' if options.skip_questions} --skip-migrations" end end |
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
15 16 17 |
# File 'lib/generators/mdwa/user_scaffold/user_scaffold_generator.rb', line 15 def model @model end |
#specific_model ⇒ Object
Returns the value of attribute specific_model.
15 16 17 |
# File 'lib/generators/mdwa/user_scaffold/user_scaffold_generator.rb', line 15 def specific_model @specific_model end |
Instance Method Details
#controller_and_view ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/generators/mdwa/user_scaffold/user_scaffold_generator.rb', line 130 def controller_and_view return nil if .only_diff_migration return nil if .skip_interface # controllers @inherit_controller = 'A::BackendController' if @model.space == 'a' template "controllers/#{'ajax_' if options.ajax}controller.rb", "app/controllers/#{@model.space}/#{@model.plural_name}_controller.rb" # views - update only template 'views/update.js.erb', "app/views/#{@model.space}/#{@model.plural_name}/update.js.erb" end |
#migration_override ⇒ Object
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 |
# File 'lib/generators/mdwa/user_scaffold/user_scaffold_generator.rb', line 68 def migration_override # override model attributes to not allow field duplicity (causing errors) @model.attributes = [] attributes.each do |attribute| attr = MDWA::Generators::ModelAttribute.new( attribute ) # add to model attributes # if it's not predefined in devise # if it's a belongs_to or nested_one association if (!attr.references? and !@predefined_fields.include?(attribute.split(':').first)) or ( (attr.belongs_to? or attr.nested_one?) and User.column_names.to_a.select {|user_attr| user_attr == attr.name.foreign_key}.count.zero? ) @model.add_attribute attr end end migration_template 'migrate.erb', "db/migrate/add_#{@model.attributes.collect{|a| a.name}.join('_')}_to_users" unless @model.attributes.empty? # include type in db:seed append_file 'db/seeds/site.rb' do "\n\nPermission.create( :name => '#{@model.singular_name}' ) if Permission.find_by_name('#{@model.singular_name}').nil?" end # run rake db:seeds if !.skip_questions and yes?('Run rake db:seed to create permission type?') rake 'db:migrate' rake 'db:seed' end end |
#model_override ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/generators/mdwa/user_scaffold/user_scaffold_generator.rb', line 99 def model_override return nil if .only_diff_migration # locate the mdwa user to discover the roles require_all "#{MDWA::DSL::USERS_PATH}#{@model.singular_name}.rb" @mdwa_user = MDWA::DSL.user(@model.name) if @mdwa_user.nil? @roles = [@model.name] else @roles = @mdwa_user.user_roles end # model override model_path = (@model.specific?) ? "app/models/#{@model.specific_model.space}/#{@model.specific_model.singular_name}.rb" : "app/models/#{@model.space}/#{@model.singular_name}.rb" gsub_file model_path, 'ActiveRecord::Base', 'User' inject_into_class model_path, @model.model_class do inj = [] @roles.each do |role| inj << "\n\n\tafter_create :create_#{role.underscore}_permission\n" inj << "\tdef create_#{role.underscore}_permission" inj << "\t\t#{role.underscore}_permission = Permission.find_by_name('#{role.underscore}')" inj << "\t\t#{role.underscore}_permission = Permission.create(:name => '#{role.underscore}') if #{role.underscore}_permission.nil?" inj << "\t\tself.permissions.push #{role.underscore}_permission" inj << "\tend" end inj.join("\n") end end |
#run_rake_db_migrate ⇒ Object
144 145 146 147 148 |
# File 'lib/generators/mdwa/user_scaffold/user_scaffold_generator.rb', line 144 def run_rake_db_migrate if !.skip_rake_migrate and !.skip_migrations and !.only_interface rake('db:migrate') if !.skip_questions and yes? 'Run rake db:migrate?' end end |