Sha256: 22ef23a5ca120ea13226b71d3f1c6385f1bfe77839c25b4645fd16ac99d83482

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

require 'active_support'

module Quickery
  module ActiveRecordExtensions
    module DSL
      def self.included(base)
        base.extend ClassMethods
        base.include InstanceMethods
      end

      module ClassMethods
        def quickery(&block)
          association_builder = AssociationBuilder.new(model: self)
          association_builder.instance_exec(&block)
        end
      end

      module InstanceMethods
        def recreate_quickery_cache!
          self.class.quickery_builders.each do |depender_column_name, quickery_builder|
            new_value = determine_quickery_value(depender_column_name)
            update_columns(depender_column_name => new_value)
          end

          true
        end

        def determine_quickery_value(depender_column_name)
          quickery_builder = self.class.quickery_builders[depender_column_name]

          raise ArgumentError, "No defined quickery builder for #{depender_column_name}. Defined values are #{self.class.quickery_builders.keys}" unless quickery_builder

          dependee_record = quickery_builder.first_association_builder._quickery_dependee_record(self)
          dependee_record.send(quickery_builder.dependee_column_name)
        end
      end
    end
  end
end

ActiveSupport.on_load(:active_record) do
  include Quickery::ActiveRecordExtensions::DSL
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
quickery-0.1.4 lib/quickery/active_record_extensions/dsl.rb
quickery-0.1.3 lib/quickery/active_record_extensions/dsl.rb
quickery-0.1.2 lib/quickery/active_record_extensions/dsl.rb