module Effective::Generators::Helpers
Protected Instance Methods
crud_actions()
click to toggle source
# File lib/generators/effective/helpers.rb, line 29 def crud_actions %w(index new create show edit update destroy) end
invokable(attributes)
click to toggle source
Turns the GeneratedAttribute or Effective::Attribute into an array of strings
# File lib/generators/effective/helpers.rb, line 71 def invokable(attributes) attributes.map { |name, (type, _)| "#{name}:#{type}" } end
invoked_actions()
click to toggle source
–actions crud another –actions crud-show another
# File lib/generators/effective/helpers.rb, line 39 def invoked_actions actions = (respond_to?(:actions) ? self.actions : options.actions) actions = Array(actions).flat_map { |arg| arg.gsub('[', '').gsub(']', '').split(',') } crudish = actions.find { |action| action.start_with?('crud') } if crudish actions = crud_actions + (actions - [crudish]) crudish.split('-').each { |except| actions.delete(except) } end actions end
invoked_attributes()
click to toggle source
As per the command line invoked actions. These are Rails Generated Attributes { :name => [:string], … }
# File lib/generators/effective/helpers.rb, line 55 def invoked_attributes if respond_to?(:attributes) attributes.inject({}) { |h, att| h[att.name.to_sym] = [att.type]; h } else Array(options.attributes).compact.inject({}) do |h, att| (name, type) = att.split(':') h[name.to_sym] = [type.to_sym] if name && type; h end end end
invoked_attributes_args()
click to toggle source
# File lib/generators/effective/helpers.rb, line 66 def invoked_attributes_args invoked_attributes.present? ? (['--attributes'] + invokable(invoked_attributes)) : [] end
non_crud_actions()
click to toggle source
# File lib/generators/effective/helpers.rb, line 33 def non_crud_actions invoked_actions - crud_actions end
resource()
click to toggle source
# File lib/generators/effective/helpers.rb, line 25 def resource @resource ||= Effective::Resource.new(name) end
resource_attributes(all: false)
click to toggle source
# File lib/generators/effective/helpers.rb, line 75 def resource_attributes(all: false) with_resource_tenant do klass_attributes = resource.klass_attributes(all: all) if klass_attributes.blank? if ActiveRecord::Migration.respond_to?(:check_pending!) pending = (ActiveRecord::Migration.check_pending! rescue true) else pending = ActiveRecord::Migrator.new(:up, ActiveRecord::Migrator.migrations(ActiveRecord::Migrator.migrations_paths)).pending_migrations.present? end if pending migrate = ask("Unable to read the attributes of #{resource.klass || resource.name}. There are pending migrations. Run db:migrate now? [y/n]") system('bundle exec rake db:migrate') if migrate.to_s.include?('y') end klass_attributes = resource.klass_attributes(all: all) end klass_attributes.presence || resource.model_attributes(all: all) end end
resource_valid?()
click to toggle source
This is kind of a validate for the resource
# File lib/generators/effective/helpers.rb, line 8 def resource_valid? if resource.klass.blank? say_status(:error, "Unable to find resource klass from #{name}", :red) return false end true end
with_resource_tenant() { || ... }
click to toggle source
# File lib/generators/effective/helpers.rb, line 17 def with_resource_tenant(&block) if defined?(Tenant) && resource.tenant.present? Tenant.as(resource.tenant) { yield } else yield end end