Module: ActsAs::ClassMethods
- Defined in:
- lib/acts_as.rb
Instance Method Summary collapse
- #acts_as(association, with: [], prefix: [], **options) ⇒ Object
- #acts_as_fields ⇒ Object
- #acts_as_fields_match(method) ⇒ Object
- #expand_hash_conditions_for_aggregates(attrs) ⇒ Object
- #where(opts = :chain, *rest) ⇒ Object
Instance Method Details
#acts_as(association, with: [], prefix: [], **options) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/acts_as.rb', line 36 def acts_as(association, with: [], prefix: [], **) belongs_to(association, **.merge(autosave: true)) define_method(association) do |*args| acted = super(*args) || send("build_#{association}", *args) acted.save if persisted? && acted.new_record? acted end if (association_class = ([:class_name] || association).to_s.camelcase.constantize).table_exists? whitelist_and_delegate_fields(association_class, association, prefix, with) override_method_missing end end |
#acts_as_fields ⇒ Object
50 51 52 |
# File 'lib/acts_as.rb', line 50 def acts_as_fields @acts_as_fields ||= {} end |
#acts_as_fields_match(method) ⇒ Object
54 55 56 57 58 |
# File 'lib/acts_as.rb', line 54 def acts_as_fields_match(method) acts_as_fields.select do |association, fields| fields.select { |f| method.to_s.include?(f) }.any? end.keys.first end |
#expand_hash_conditions_for_aggregates(attrs) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/acts_as.rb', line 72 def (attrs) attrs = super(attrs) = {} attrs.each do |attr, value| if (association = acts_as_fields_match(attr)) && !self.columns.map(&:name).include?(attr.to_s) [new.send(association).class.table_name] = { attr => value } else [attr] = value end end end |
#where(opts = :chain, *rest) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/acts_as.rb', line 60 def where(opts = :chain, *rest) relation = super #TODO support nested attribute joins like Guns.where(rebels: {strength: 10})) # for now, only first level joins will happen automagically if opts.is_a? Hash detected_associations = opts.keys.map {|attr| acts_as_fields_match(attr) } .reject {|attr| attr.nil?} return relation.joins(detected_associations) if detected_associations.any? end relation end |