Sha256: 94831e6ae45b2347750d2b04d431c3165fc13cabcbd7778cb606e8105252ccb0

Contents?: true

Size: 1.56 KB

Versions: 6

Compression:

Stored size: 1.56 KB

Contents

module ActsAsRecursiveTree
  module Options
    module Values
      class Base
        attr_reader :value, :config

        def initialize(value, config)
          @value  = value
          @config = config
        end

        def prepared_value
          value
        end

        def apply_to(attribute)

        end

        def apply_negated_to(attribute)

        end
      end

      class SingleValue < Base
        def apply_to(attribute)
          attribute.eq(prepared_value)
        end

        def apply_negated_to(attribute)
          attribute.not_eq(prepared_value)
        end
      end

      class ActiveRecord < SingleValue
        def prepared_value
          value.id
        end
      end

      class MultiValue < Base
        def apply_to(attribute)
          attribute.in(prepared_value)
        end

        def apply_negated_to(attribute)
          attribute.not_in(prepared_value)
        end
      end

      class Relation < MultiValue
        def prepared_value
          select_manager = value.arel
          select_manager.projections.clear
          select_manager.project(select_manager.froms.last[config.primary_key])
        end
      end

      def self.create(value, config = nil)
        klass = case value
        when ::Numeric, ::String
          SingleValue
        when ::ActiveRecord::Relation
          Relation
        when Enumerable
          MultiValue
        when ::ActiveRecord::Base
          ActiveRecord
        else
          raise "#{value.class} is not supported"
        end

        klass.new(value, config)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
acts_as_recursive_tree-2.2.0 lib/acts_as_recursive_tree/options/values.rb
acts_as_recursive_tree-2.1.1 lib/acts_as_recursive_tree/options/values.rb
acts_as_recursive_tree-2.1.0 lib/acts_as_recursive_tree/options/values.rb
acts_as_recursive_tree-2.0.2 lib/acts_as_recursive_tree/options/values.rb
acts_as_recursive_tree-2.0.1 lib/acts_as_recursive_tree/options/values.rb
acts_as_recursive_tree-2.0.0 lib/acts_as_recursive_tree/options/values.rb