Module: BraintreeRails::CollectionAssociation::InstanceMethods
- Defined in:
- lib/braintree_rails/collection_association.rb
Instance Method Summary collapse
- #build(params = {}) ⇒ Object
- #create(params = {}) ⇒ Object
- #create!(params = {}) ⇒ Object
- #find(id = nil, &block) ⇒ Object
- #initialize(collection) ⇒ Object
- #load! ⇒ Object
- #model_class ⇒ Object
- #reload ⇒ Object
Instance Method Details
#build(params = {}) ⇒ Object
27 28 29 |
# File 'lib/braintree_rails/collection_association.rb', line 27 def build(params = {}) model_class.new(.merge(params)) end |
#create(params = {}) ⇒ Object
31 32 33 |
# File 'lib/braintree_rails/collection_association.rb', line 31 def create(params = {}) build(params).tap { |model| push(model) if model.save } end |
#create!(params = {}) ⇒ Object
35 36 37 |
# File 'lib/braintree_rails/collection_association.rb', line 35 def create!(params = {}) build(params).tap { |model| push(model) if model.save! } end |
#find(id = nil, &block) ⇒ Object
23 24 25 |
# File 'lib/braintree_rails/collection_association.rb', line 23 def find(id = nil, &block) id.nil? ? super(&block) : super() { |model| model.id == id } end |
#initialize(collection) ⇒ Object
19 20 21 |
# File 'lib/braintree_rails/collection_association.rb', line 19 def initialize(collection) super(self.collection = (collection || [])) end |
#load! ⇒ Object
43 44 45 46 47 |
# File 'lib/braintree_rails/collection_association.rb', line 43 def load! return if loaded self.loaded = true __setobj__(collection.map{|model| model_class.new(model)}) end |
#model_class ⇒ Object
39 40 41 |
# File 'lib/braintree_rails/collection_association.rb', line 39 def model_class self.class.name.singularize.constantize end |
#reload ⇒ Object
49 50 51 52 53 |
# File 'lib/braintree_rails/collection_association.rb', line 49 def reload self.loaded = false self.load! self end |