Sha256: 918f16e09a045b230259c8b83678149595ba5150f788c039595d094f1c28d3f2
Contents?: true
Size: 1.66 KB
Versions: 2
Compression:
Stored size: 1.66 KB
Contents
module BarkestCore ## # This module allows you to define defaults for a model's associations. # # While models can be setup with static defaults, this allows you to # provide dynamic defaults. You must define the +association_defaults+ method # within the association block for it to work though. This method should process # the attributes in whatever way it needs. In the example below, we would actually # want to look for +org+ and +access+ attributes before overriding the +org_id+ and # +access_id+ attributes. # # This method will override the +new+, +build+, +create+, and +create!+ methods # of the association to ensure the dynamic defaults are processed properly. # # class User # ... # has_many :accesses, ->{ extending BarkestCore::AssociationWithDefaults } do # def association_defaults(attributes = {}) # { # org_id: proxy_association.owner.current_organization.id, # access_id: Access.default.id # }.merge(attributes || {}) # end # end # ... # end # module AssociationWithDefaults # def association_defaults(attributes = {}) # attributes[:some_value] ||= some_default # attributes # end def new(attributes = {}) super association_defaults(attributes) end def build(*args) args << {} if args.blank? args = args.map { |a| a.is_a?(Hash) ? association_defaults(a) : a } super(*args) end def create(attributes = {}) super association_defaults(attributes) end def create!(attributes = {}) super association_defaults(attributes) end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
barkest_core-1.5.4.0 | lib/barkest_core/concerns/association_with_defaults.rb |
barkest_core-1.5.3.0 | lib/barkest_core/concerns/association_with_defaults.rb |