class Tramway::Admin::Generators::ModelGenerator

Constants

ATTRIBUTE_TYPE_MAPPING
COLLECTION_ATTRIBUTE_LIMIT
DEFAULT_FIELD_TYPE
READ_ONLY_ATTRIBUTES

Public Instance Methods

run_decorator_generator() click to toggle source
# File lib/tramway/admin/generators/model_generator.rb, line 25
def run_decorator_generator
  template(
    'decorator.rb.erb',
    Rails.root.join("app/decorators/#{file_path}_decorator.rb"),
  )
end
run_forms_generator() click to toggle source
# File lib/tramway/admin/generators/model_generator.rb, line 32
def run_forms_generator
  template(
    'form.rb.erb',
    Rails.root.join("app/forms/#{user_role}/#{file_path}_form.rb"),
  )
end

Private Instance Methods

association_type(attribute) click to toggle source
# File lib/tramway/admin/generators/model_generator.rb, line 82
def association_type(attribute)
  relationship = klass.reflections[attribute.to_s]
  if relationship.has_one?
    :has_one
  elsif relationship.collection?
    :has_many
  elsif relationship.polymorphic?
    :polymorphic
  else
    :belongs_to
  end
end
attributes() click to toggle source
# File lib/tramway/admin/generators/model_generator.rb, line 45
def attributes
  # TODO: this for model associations, but they probably should be handled differently
  # klass.reflections.keys +

  klass.columns.map(&:name) -
    redundant_attributes
end
column_type_for_attribute(attr) click to toggle source
# File lib/tramway/admin/generators/model_generator.rb, line 95
def column_type_for_attribute(attr)
  column_types(attr)
end
column_types(attr) click to toggle source
# File lib/tramway/admin/generators/model_generator.rb, line 99
def column_types(attr)
  klass.columns.find { |column| column.name == attr }.try(:type)
end
form_attributes() click to toggle source
# File lib/tramway/admin/generators/model_generator.rb, line 53
def form_attributes
  attributes - READ_ONLY_ATTRIBUTES
end
form_type(attribute) click to toggle source
# File lib/tramway/admin/generators/model_generator.rb, line 57
def form_type(attribute)
  type = column_type_for_attribute(attribute.to_s)

  if type
    ATTRIBUTE_TYPE_MAPPING.fetch(type, DEFAULT_FIELD_TYPE)
  # else
  #   association_type(attribute)
  end
end
klass() click to toggle source
# File lib/tramway/admin/generators/model_generator.rb, line 103
def klass
  @klass ||= Object.const_get(class_name)
end
redundant_attributes() click to toggle source
# File lib/tramway/admin/generators/model_generator.rb, line 67
def redundant_attributes
  klass.reflections.keys.flat_map do |relationship|
    redundant_attributes_for(relationship)
  end.compact
end
redundant_attributes_for(relationship) click to toggle source
# File lib/tramway/admin/generators/model_generator.rb, line 73
def redundant_attributes_for(relationship)
  case association_type(relationship)
  when :polymorphic
    [relationship + '_id', relationship + '_type']
  when :belongs_to
    relationship + '_id'
  end
end
user_role() click to toggle source
# File lib/tramway/admin/generators/model_generator.rb, line 41
def user_role
  options[:user_role]
end