Class: Carload::ModelSpec
- Inherits:
-
Object
- Object
- Carload::ModelSpec
- Defined in:
- lib/carload/model_spec.rb
Constant Summary collapse
- SkippedAttributes =
[ 'id', 'created_at', 'updated_at', 'encrypted_password', 'reset_password_token', 'reset_password_sent_at', 'remember_created_at', 'sign_in_count', 'current_sign_in_at', 'last_sign_in_at', 'current_sign_in_ip', 'last_sign_in_ip' ].freeze
Instance Attribute Summary collapse
-
#associations ⇒ Object
Returns the value of attribute associations.
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#default ⇒ Object
Returns the value of attribute default.
-
#index_page ⇒ Object
Returns the value of attribute index_page.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #changed?(spec) ⇒ Boolean
-
#initialize(model_class = nil) ⇒ ModelSpec
constructor
A new instance of ModelSpec.
- #process_associaitons ⇒ Object
Constructor Details
#initialize(model_class = nil) ⇒ ModelSpec
Returns a new instance of ModelSpec.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# 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 |
Instance Attribute Details
#associations ⇒ Object
Returns the value of attribute associations.
3 4 5 |
# File 'lib/carload/model_spec.rb', line 3 def associations @associations end |
#attributes ⇒ Object
Returns the value of attribute attributes.
3 4 5 |
# File 'lib/carload/model_spec.rb', line 3 def attributes @attributes end |
#default ⇒ Object
Returns the value of attribute default.
3 4 5 |
# File 'lib/carload/model_spec.rb', line 3 def default @default end |
#index_page ⇒ Object
Returns the value of attribute index_page.
3 4 5 |
# File 'lib/carload/model_spec.rb', line 3 def index_page @index_page end |
#klass ⇒ Object
Returns the value of attribute klass.
3 4 5 |
# File 'lib/carload/model_spec.rb', line 3 def klass @klass end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/carload/model_spec.rb', line 3 def name @name end |
Instance Method Details
#changed?(spec) ⇒ Boolean
74 75 76 77 78 79 |
# File 'lib/carload/model_spec.rb', line 74 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 ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/carload/model_spec.rb', line 42 def process_associaitons @associations.each_value do |association| reflection = association[:reflection] if join_table = reflection.[: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.[:polymorphic] ActiveRecord::Base.descendants.each do |_model| next if _model.name == 'ApplicationRecord' or _model.name.underscore == @name.to_s _model.reflect_on_all_associations.each do |_reflection| next unless _reflection.[: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 |