Class: Carload::ModelSpec
- Inherits:
-
Object
- Object
- Carload::ModelSpec
- Includes:
- AssociationPipelines
- 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
- AssociationTypes =
{ ActiveRecord::Reflection::BelongsToReflection => :belongs_to, ActiveRecord::Reflection::HasOneReflection => :has_one, ActiveRecord::Reflection::HasManyReflection => :has_many, ActiveRecord::Reflection::ThroughReflection => :through }
Instance Attribute Summary collapse
-
#associated_models ⇒ Object
Returns the value of attribute associated_models.
-
#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
- #handle_association(association, options = {}) ⇒ Object
-
#initialize(model_class = nil) ⇒ ModelSpec
constructor
A new instance of ModelSpec.
- #revise! ⇒ Object
Methods included from AssociationPipelines
#association_pipelines, #pipeline_1, #pipeline_2, #pipeline_3
Constructor Details
#initialize(model_class = nil) ⇒ ModelSpec
Returns a new instance of ModelSpec.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/carload/model_spec.rb', line 23 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] ||= [] @associated_models = {} if model_class @name = model_class.name.underscore @klass = model_class @attributes[:permitted] = (model_class.column_names - SkippedAttributes).map(&:to_sym) # Handle model associations. model_class.reflect_on_all_associations.each do |association| handle_association association end @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 end end |
Instance Attribute Details
#associated_models ⇒ Object
Returns the value of attribute associated_models.
5 6 7 |
# File 'lib/carload/model_spec.rb', line 5 def associated_models @associated_models end |
#attributes ⇒ Object
Returns the value of attribute attributes.
5 6 7 |
# File 'lib/carload/model_spec.rb', line 5 def attributes @attributes end |
#default ⇒ Object
Returns the value of attribute default.
5 6 7 |
# File 'lib/carload/model_spec.rb', line 5 def default @default end |
#index_page ⇒ Object
Returns the value of attribute index_page.
5 6 7 |
# File 'lib/carload/model_spec.rb', line 5 def index_page @index_page end |
#klass ⇒ Object
Returns the value of attribute klass.
5 6 7 |
# File 'lib/carload/model_spec.rb', line 5 def klass @klass end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/carload/model_spec.rb', line 5 def name @name end |
Instance Method Details
#changed?(spec) ⇒ Boolean
48 49 50 51 52 53 |
# File 'lib/carload/model_spec.rb', line 48 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 |
#handle_association(association, options = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/carload/model_spec.rb', line 69 def handle_association association, = {} begin _association = (association.delegate_reflection rescue nil) || association name = (_association.klass.name.underscore.to_sym rescue nil) || _association.name association_type = AssociationTypes[_association.class] polymorphic = association.[:polymorphic] || association.[:as] foreign_key = @klass.column_names.include?("#{(_association.klass.name.underscore rescue nil) || _association.name}_id") join_table = association.[:through].to_s.singularize.to_sym if association.[:through] @associated_models[name] = { name: name, association_type: association_type, polymorphic: polymorphic, foreign_key: foreign_key, join_table: join_table, choose_by: nil }.merge @associated_models[name] || {} association_pipelines.each { |pipeline| send pipeline, association } # Delete join-table model! if association.[:through] @associated_models.delete association.[:through] @associated_models.delete association.[:through].to_s.singularize.to_sym end rescue => e raise e unless [:rescue] raise e if not e&.original_exception&.class == PG::UndefinedTable and not e.class == ActiveRecord::NoDatabaseError end end |
#revise! ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/carload/model_spec.rb', line 55 def revise! # Handle associated models if necessary. @associated_models.each_value do |associated_model| next unless associated_model[:choose_by] if associated_model[:association_type] == :has_many show_name = [:pluck, associated_model[:name].to_s.pluralize.to_sym, associated_model[:choose_by]] else show_name = "#{associated_model[:name]}.#{associated_model[:choose_by]}" end @index_page[:shows][:attributes].delete_if { |x| x == :"#{associated_model[:name]}_id" } @index_page[:shows][:attributes] << show_name end end |