Sha256: 2c32eb2ef65eec8ac086e1d941c357e09d3b9a453d5247e07c30f97ec4a510e3

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

module BraintreeRails
  module Association
    module ClassMethods
      def lazy_load(methods)
        methods.each do |method|
          define_method method do |*args, &block|
            load!
            super(*args, &block)
          end
        end
      end
    end

    module InstanceMethods
      def initialize(models)
        super(Array(models).map{|model| model_class.new(model)})
      end

      def find(id = nil, &block)
        id.nil? ? super(&block) : super() { |model| model.id == id }
      end

      def build(params)
        model_class.new(params.merge(default_options))
      end

      def create(params)
        build(params).tap { |model| push(model) if model.save }
      end

      def create!(params)
        build(params).tap { |model| push(model) if model.save! }
      end

      def model_class
        self.class.name.singularize.constantize
      end
    end

    def self.included(receiver)
      receiver.extend         ClassMethods
      receiver.send :include, InstanceMethods
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
braintree-rails-0.4.2 lib/braintree_rails/association.rb