class Carload::ModelSpec

Constants

SkippedAttributes

Attributes

associations[RW]
attributes[RW]
default[RW]
index_page[RW]
klass[RW]
name[RW]

Public Class Methods

new(model_class = nil) click to toggle source
# File lib/carload/model_spec.rb, line 14
def initialize model_class = nil
  @default = false
  @attributes = ExtendedHash.new
  @index_page = ExtendedHash.new
  @index_page[:shows] = ExtendedHash.new
  @index_page[:shows][:attributes] ||= []
  @index_page[:searches] = ExtendedHash.new
  @index_page[:searches][:attributes] ||= []
  @associations ||= {}
  if model_class
    @name = model_class.name.underscore
    @klass = model_class
    @attributes[:permitted] = (model_class.column_names - SkippedAttributes).map(&:to_sym)
    @attributes[:permitted].each do |attribute|
      next if attribute.class == Hash
      @index_page[:shows][:attributes] << attribute
      @index_page[:searches][:attributes] << { name: attribute.to_sym, term: :cont }
    end
    model_class.reflect_on_all_associations.each do |reflection|
      @associations[reflection.name] = {
        reflection: reflection,
        choose_by: nil
      }
    end
    process_associaitons
  end
end

Public Instance Methods

changed?(spec) click to toggle source
# File lib/carload/model_spec.rb, line 75
def changed? spec
  not @default == spec.default or
  not @attributes[:permitted] == spec.attributes[:permitted] or
  not @index_page[:shows][:attributes] == spec.index_page[:shows][:attributes] or
  not @index_page[:searches][:attributes] == spec.index_page[:searches][:attributes]
end
process_associaitons() click to toggle source
# File lib/carload/model_spec.rb, line 42
def process_associaitons
  @associations.each_value do |association|
    reflection = association[:reflection]
    if join_table = reflection.options[:through] and @associations.has_key? join_table
      # Filter join table.
      @associations.select { |k, v| v[:reflection].name == join_table }.values.first[:filtered] = true
      # Permit foreign id.
      case reflection.delegate_reflection
      when ActiveRecord::Reflection::HasOneReflection
        @attributes[:permitted] << :"#{reflection.delegate_reflection.name}_id"
      when ActiveRecord::Reflection::HasManyReflection
        @attributes[:permitted] << { :"#{reflection.delegate_reflection.name.to_s.singularize}_ids" => [] }
      end
    elsif reflection.options[:polymorphic]
      Dir.glob("#{Rails.root}/app/models/*.rb").each do |filename|
        _model = File.basename(filename).gsub('.rb', '').camelize.constantize rescue next
        next if _model.name == 'ApplicationRecord' or _model.name.underscore == @name.to_s
        _model.reflect_on_all_associations.each do |_reflection|
          next unless _reflection.options[:as] == reflection.name
          if association.has_key? :attributes
            association[:attributes] = association[:attributes] & _model.column_names
          else
            association[:attributes] = _model.column_names - SkippedAttributes
          end
          association[:instance_models] ||= []
          association[:instance_models] << _model.name.underscore.to_sym
        end
      end
      association[:attributes] = association[:attributes].map(&:to_sym)
    end
  end
end