lib/generators/effective/helpers.rb in effective_developer-0.0.10 vs lib/generators/effective/helpers.rb in effective_developer-0.1.1

- old
+ new

@@ -2,10 +2,14 @@ module Generators module Helpers protected + def resource + @resource ||= Effective::Resource.new(name) + end + def crud_actions %w(index new create show edit update destroy) end # --actions crud another @@ -22,99 +26,40 @@ end actions end + # As per the command line invoked actions. These are Rails Generated Attributes def invoked_attributes if respond_to?(:attributes) - attributes.map { |att| "#{att.name}:#{att.type}" } + attributes else - Array(options.attributes).compact + Array(options.attributes).compact.map { |att| Rails::Generators::GeneratedAttribute.parse(att) } end end def invoked_attributes_args - invoked_attributes.present? ? (['--attributes'] + invoked_attributes) : [] + invoked_attributes.present? ? (['--attributes'] + invokable(invoked_attributes)) : [] end - def klass_attributes(verbose: true) - klass = class_name.safe_constantize - return [] unless klass - - begin - attributes = klass.new().attributes - rescue ActiveRecord::StatementInvalid => e - pending = ActiveRecord::Migrator.new(:up, ActiveRecord::Migrator.migrations(ActiveRecord::Migrator.migrations_paths)).pending_migrations.present? - - if e.message.include?('PG::UndefinedTable') && pending - migrate = ask("Unable to read the attributes of #{class_name}. There are pending migrations. Run db:migrate now? [y/n]") - system('bundle exec rake db:migrate') if migrate.to_s.include?('y') - end - end - - begin - attributes = klass.new().attributes - rescue => e - puts "Unable to call #{class_name}.new().attributes. Continuing with empty attributes." if verbose - return [] - end - - attribute_names = attributes.keys - [klass.primary_key, 'created_at', 'updated_at'] - attribute_names -= ['site_id'] if klass.respond_to?(:is_site_specific) - - attribute_names.map do |attr| - if klass.respond_to?(:column_for_attribute) # Rails 4+ - "#{attr}:#{klass.column_for_attribute(attr).try(:type) || 'string'}" - else - "#{attr}:#{klass.columns_hash[attr].try(:type) || 'string'}" - end - end + # Turns the GeneratedAttribute or Effective::Attribute into an array of strings + def invokable(attributes) + attributes.map { |att| "#{att.name}:#{att.type}" } end - def parent_class_name - options[:parent] || (Rails::VERSION::MAJOR > 4 ? 'ApplicationRecord' : 'ActiveRecord::Base') - end + def resource_attributes + klass_attributes = resource.klass_attributes - # We handle this a bit different than the regular scaffolds - def assign_names!(name) - @class_path = (name.include?('/') ? name[(name.rindex('/')+1)..-1] : name).split('::') - @class_path.map!(&:underscore) - @class_path[@class_path.length-1] = @class_path.last.singularize # Always singularize - @file_name = @class_path.pop - end + if klass_attributes.blank? + pending = ActiveRecord::Migrator.new(:up, ActiveRecord::Migrator.migrations(ActiveRecord::Migrator.migrations_paths)).pending_migrations.present? - def namespaces - @namespaces ||= namespace_path.split('/') - end + migrate = ask("Unable to read the attributes of #{resource.klass}. There are pending migrations. Run db:migrate now? [y/n]") + system('bundle exec rake db:migrate') if migrate.to_s.include?('y') - # admin/effective::things => 'admin' - # effective::things => '' - def namespace_path - name.include?('/') ? name[0...name.rindex('/')] : '' - end - - def namespaced_class_name - if name.include?('/') - name[0...name.rindex('/')].classify + '::' + singular_name.classify.pluralize - else - singular_name.classify.pluralize + klass_attributes = resource.klass_attributes end - end - def index_path - [namespace_path.underscore.presence, plural_name, 'path'].compact.join('_') - end - - def new_path - ['new', namespace_path.underscore.presence, singular_name, 'path'].compact.join('_') - end - - def edit_path - "edit_#{show_path}" - end - - def show_path - [namespace_path.underscore.presence, singular_name, 'path', "(@#{singular_name})"].compact.join('_') + klass_attributes.presence || resource.written_attributes end end end end