Sha256: 163f75069fb2ec8de65268341c14e94924429f62e373fbacb4defb7621a1213d

Contents?: true

Size: 1.22 KB

Versions: 7

Compression:

Stored size: 1.22 KB

Contents

module Decanter
  module Extensions

    def self.included(base)
      base.extend(ClassMethods)
    end

    def decant_update(args, **options)
      self.attributes = self.class.decant(args, options)
      self.save(context: options[:context])
    end

    def decant_update!(args, **options)
      self.attributes = self.class.decant(args, options)
      self.save!(context: options[:context])
    end

    def decant(args, **options)
      self.class.decant(args, options)
    end

    module ClassMethods

      def decant_create(args, **options)
        self.new(decant(args, options))
            .save(context: options[:context])
      end

      def decant_new(args, **options)
        self.new(decant(args, options))
      end

      def decant_create!(args, **options)
        self.new(decant(args, options))
            .save!(context: options[:context])
      end

      def decant(args, options={})
        if specified_decanter = options[:decanter]
          Decanter.decanter_from(specified_decanter)
        else
          Decanter.decanter_for(self)
        end.decant(args)
      end
    end

    module ActiveRecord
      def self.enable!
        ::ActiveRecord::Base.include(Decanter::Extensions)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
decanter-0.9.1 lib/decanter/extensions.rb
decanter-0.9.0 lib/decanter/extensions.rb
decanter-0.8.2 lib/decanter/extensions.rb
decanter-0.8.1 lib/decanter/extensions.rb
decanter-0.8.0 lib/decanter/extensions.rb
decanter-0.7.2 lib/decanter/extensions.rb
decanter-0.7.1 lib/decanter/extensions.rb