class Effective::Generators::FormGenerator

Public Instance Methods

assign_attributes() click to toggle source
# File lib/generators/effective/form_generator.rb, line 27
def assign_attributes
  @attributes = invoked_attributes.presence || resource_attributes
  self.class.send(:attr_reader, :attributes)
end
create_flat_form() click to toggle source
# File lib/generators/effective/form_generator.rb, line 36
def create_flat_form
  with_resource_tenant do
    if options[:tabbed] == 'false'
      template 'forms/flat/_form.html.haml', resource.view_file('form', partial: true)
    end
  end
end
create_tabbed_form() click to toggle source
# File lib/generators/effective/form_generator.rb, line 44
def create_tabbed_form
  with_resource_tenant do
    if options[:tabbed] == 'true'
      template 'forms/tabbed/_form.html.haml', resource.view_file('form', partial: true)
      template 'forms/tabbed/_form_resource.html.haml', resource.view_file("form_#{resource.name}", partial: true)
    end
  end
end
invoke_form() click to toggle source
# File lib/generators/effective/form_generator.rb, line 32
def invoke_form
  say_status :invoke, :form, :white
end
validate_resource() click to toggle source
# File lib/generators/effective/form_generator.rb, line 23
def validate_resource
  exit unless resource_valid?
end

Protected Instance Methods

form_for() click to toggle source
# File lib/generators/effective/form_generator.rb, line 55
def form_for
  if resource.namespaces.blank?
    resource.name
  else
    '[' + resource.namespaces.map { |ns| ':' + ns }.join(', ') + ', ' + resource.name + ']'
  end
end
render_field(attribute, depth: 0) click to toggle source
# File lib/generators/effective/form_generator.rb, line 63
def render_field(attribute, depth: 0)
  b = binding

  b.local_variable_set(:resource, resource)

  partial = case attribute
  when (ActiveRecord::Reflection::BelongsToReflection rescue false)
    b.local_variable_set(:reference, attribute)
    'belongs_to'
  when (ActiveRecord::Reflection::HasManyReflection rescue false)
    b.local_variable_set(:nested_resource, attribute)
    'nested_resource'
  when Effective::Resource
    b.local_variable_set(:nested_resource, attribute)
    'nested_resource'
  else # [:name, [:string]]
    name = attribute.first.to_s
    datatype = ((attribute.last || []).first || :string).to_s

    b.local_variable_set(:attribute, name)

    if datatype == 'string' && (resource.klass || NilClass).const_defined?(name.pluralize.upcase)
      attribute_constant = "#{resource.klass.name}::#{name.pluralize.upcase}"
      b.local_variable_set(:attribute_constant, attribute_constant)

      'select_constant'
    elsif datatype == :string && (resource.klass || NilClass).const_defined?(name.singularize.upcase)
      attribute_constant = "#{resource.klass.name}::#{name.singularize.upcase}"
      b.local_variable_set(:attribute_constant, attribute_constant)

      'select_constant'
    elsif name.include?('price')
      'price'
    elsif name.include?('_url')
      'url'
    elsif name.include?('email')
      'email'
    else
      datatype
    end
  end

  html = ERB.new(
    File.read("#{File.dirname(__FILE__)}/../../scaffolds/forms/fields/_field_#{partial}.html.haml")
  ).result(b).split("\n").map { |line| ('  ' * depth) + line }

  html.length > 1 ? (html.join("\n") + "\n") : html.join
end