Class: Hist::HistConfig
- Inherits:
-
Object
- Object
- Hist::HistConfig
- Defined in:
- app/models/hist/hist_config.rb
Instance Method Summary collapse
- #all_associations(klass:, type: nil, exclude_through: false) ⇒ Object
-
#associations(obj: nil, klass: nil, exclude_through: false) ⇒ Object
Support STI.
- #auto_version ⇒ Object
- #exclude ⇒ Object
- #include ⇒ Object
-
#initialize(associations: nil, model:, max_versions:, max_pendings:, include: [], exclude: [], auto_version:) ⇒ HistConfig
constructor
A new instance of HistConfig.
- #max_pendings ⇒ Object
- #max_versions ⇒ Object
- #model ⇒ Object
- #options ⇒ Object
- #update_associations_on_save(klass:, assoc:) ⇒ Object
- #valid_association(klass:, assoc:, exclude_through: false) ⇒ Object
Constructor Details
#initialize(associations: nil, model:, max_versions:, max_pendings:, include: [], exclude: [], auto_version:) ⇒ HistConfig
Returns a new instance of HistConfig.
4 5 6 7 8 9 10 11 12 |
# File 'app/models/hist/hist_config.rb', line 4 def initialize(associations:nil, model:, max_versions:, max_pendings:, include: [], exclude: [], auto_version:) @associations = associations @model = model @max_versions = max_versions @max_pendings = max_pendings @include = include @exclude = exclude @auto_version = auto_version end |
Instance Method Details
#all_associations(klass:, type: nil, exclude_through: false) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'app/models/hist/hist_config.rb', line 80 def all_associations(klass:, type: nil, exclude_through:false) if type.nil? associations = klass.reflect_on_all_associations else associations = klass.reflect_on_all_associations(type) end if exclude_through through_associations = associations.select { |assoc| assoc.class == ActiveRecord::Reflection::ThroughReflection} through_associations.each do |t_assoc| assoc_to_delete = associations.select { |assoc| assoc.name == t_assoc.[:through]} associations.delete(assoc_to_delete[0]) if assoc_to_delete.present? end end associations end |
#associations(obj: nil, klass: nil, exclude_through: false) ⇒ Object
Support STI
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/models/hist/hist_config.rb', line 39 def associations(obj: nil, klass: nil, exclude_through:false) return [] if @associations.nil? klass = obj.class if obj.present? klass = self.model.constantize if klass.nil? if @associations.present? if @associations.has_key? :all return all_associations(klass: klass, exclude_through: exclude_through) elsif @associations.has_key? :has_many return all_associations(klass: klass, type: :has_many, exclude_through: exclude_through) elsif @associations.has_key? :belongs_to return all_associations(klass: klass,type: :belongs_to, exclude_through: exclude_through) end end return_assocs = [] @associations.each do |k, _| return_assocs << (klass.reflect_on_all_associations.select { |a| a.name == k})[0] if valid_association(klass: klass, assoc: k, exclude_through: exclude_through) end return_assocs end |
#auto_version ⇒ Object
14 15 16 |
# File 'app/models/hist/hist_config.rb', line 14 def auto_version @auto_version end |
#exclude ⇒ Object
30 31 32 |
# File 'app/models/hist/hist_config.rb', line 30 def exclude @exclude end |
#include ⇒ Object
26 27 28 |
# File 'app/models/hist/hist_config.rb', line 26 def include @include end |
#max_pendings ⇒ Object
22 23 24 |
# File 'app/models/hist/hist_config.rb', line 22 def max_pendings @max_pendings end |
#max_versions ⇒ Object
18 19 20 |
# File 'app/models/hist/hist_config.rb', line 18 def max_versions @max_versions end |
#model ⇒ Object
34 35 36 |
# File 'app/models/hist/hist_config.rb', line 34 def model @model end |
#options ⇒ Object
118 119 120 |
# File 'app/models/hist/hist_config.rb', line 118 def @options end |
#update_associations_on_save(klass:, assoc:) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'app/models/hist/hist_config.rb', line 99 def update_associations_on_save(klass:, assoc:) if @associations.blank? return false end if @associations.has_key? :all return true if @associations[:all][:update_associations_on_save].nil? || @associations[:all][:update_associations_on_save] elsif @associations.has_key? :has_many return true if @associations[:has_many][:update_associations_on_save].nil? || @associations[:has_many][:update_associations_on_save] elsif @associations.has_key? :belongs_to return true if @associations[:belongs_to][:update_associations_on_save].nil? || @associations[:belongs_to][:update_associations_on_save] end item = @associations[assoc] return true if !item.nil? and (item[:update_associations_on_save].nil? || item[:update_associations_on_save]) return false end |
#valid_association(klass:, assoc:, exclude_through: false) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/models/hist/hist_config.rb', line 63 def valid_association(klass:, assoc:, exclude_through:false) all_assocs = klass.reflect_on_all_associations association_details = all_assocs.select { |a| a.name == assoc }[0] if association_details.present? if exclude_through and association_details.class == ActiveRecord::Reflection::HasManyReflection through_associations = all_assocs.select { |assoc| assoc.class == ActiveRecord::Reflection::ThroughReflection} if through_associations.present? assoc_check = through_associations.select { |assoc| assoc.[:through] == assoc } return false if assoc_check.present? end end return true end return false end |