module Effective::Generators::Helpers

Protected Instance Methods

admin_effective_scaffold?() click to toggle source
# File lib/generators/effective/helpers.rb, line 18
def admin_effective_scaffold?
  admin_scaffold? &&  effective_scaffold?
end
admin_scaffold?() click to toggle source
# File lib/generators/effective/helpers.rb, line 14
def admin_scaffold?
  Array(resource.namespaces).include?('admin')
end
basic_scaffold?() click to toggle source
# File lib/generators/effective/helpers.rb, line 22
def basic_scaffold?
  resource.klass.name.start_with?('Effective::') == false
end
crud_actions() click to toggle source
# File lib/generators/effective/helpers.rb, line 59
def crud_actions
  %w(index new create show edit update destroy)
end
effective_gem_name() click to toggle source

Returns effective_messaging if run from that directory to scaffold

# File lib/generators/effective/helpers.rb, line 31
def effective_gem_name
  path = Dir.pwd.split('/').last.to_s
  raise('effective gem name not supported') unless path.start_with?('effective_')
  path
end
effective_scaffold?() click to toggle source
# File lib/generators/effective/helpers.rb, line 26
def effective_scaffold?
  resource.klass.name.start_with?('Effective::')
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 101
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 69
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 85
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 96
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 63
def non_crud_actions
  invoked_actions - crud_actions
end
resource() click to toggle source
# File lib/generators/effective/helpers.rb, line 55
def resource
  @resource ||= Effective::Resource.new(name)
end
resource_attributes(all: false) click to toggle source
# File lib/generators/effective/helpers.rb, line 105
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 38
def resource_valid?
  if resource.klass.blank?
    say_status(:error, "Unable to find resource klass from #{name}", :red)
    return false
  end

  true
end
scaffold_path() click to toggle source

Based on the resource, effective or basic

# File lib/generators/effective/helpers.rb, line 8
def scaffold_path
  return 'admin_effective' if admin_effective_scaffold?
  return 'effective' if effective_scaffold?
  'basic'
end
with_resource_tenant() { || ... } click to toggle source
# File lib/generators/effective/helpers.rb, line 47
def with_resource_tenant(&block)
  if defined?(Tenant) && resource.tenant.present?
    Tenant.as(resource.tenant) { yield }
  else
    yield
  end
end