Sha256: dd070085cc8854e527b68292bd157deed19c533aa54b7d8e21eb100546539aeb

Contents?: true

Size: 2 KB

Versions: 26

Compression:

Stored size: 2 KB

Contents

class Refine::Filters::Criterion
  # View model that holds the state of individual criteria within the Filter query
  include ActiveModel::Model
  include ActiveModel::Attributes

  attribute :depth, :integer
  attribute :criterion, :string
  attribute :condition_id, :string
  attribute :input
  attribute :word, :string
  attribute :type, :string
  attribute :position, :integer
  attribute :uid, :string

  attr_accessor :query
  delegate :filter, to: :query, allow_nil: true

  attr_reader :condition

  def initialize(**attrs)
    super
    initialize_condition!
  end

  def validate!
    errors.clear
    return true if type == "conjunction"
    begin
      query_for_validate = filter.initial_query || filter.model.all
      condition&.apply(input, filter.table, query_for_validate)
    rescue Refine::Conditions::Errors::ConditionClauseError => e
      e.errors.each do |error|
        errors.add(:base, :invalid, message: error.full_message)
      end
    end
  end

  def condition_attributes
    result = condition&.to_array(allow_errors: true)
    result.to_h
  end

  def meta
    condition_attributes[:meta]
  end

  def selected_clause
    input[:clause]
  end

  def selected_clause_meta
    meta[:clauses].find {|c| c[:id] == selected_clause }[:meta]
  end

  def refinements
    condition.refinements_to_array
  end

  def meta_for_refinement_clause(refinement)
    refinement_meta = refinement[:meta]
    selected_clause_id = input[refinement[:id].to_sym][:clause]
    clauses = refinement_meta[:clauses]
    selected_clause = clauses.find { |clause| clause[:id] == selected_clause_id }
    selected_clause[:meta]
  end

  def component
    condition.component.underscore
  end

  private

  def initialize_condition!
    @condition = query
      .available_conditions
      .find { |condition| condition.id == condition_id }
      .dup

    if @condition
      @condition.set_filter(filter)
      filter.translate_display(@condition)
    elsif type != "conjunction"
      raise Refine::InvalidFilterError
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
refine-rails-2.12.0 app/models/refine/filters/criterion.rb
refine-rails-2.11.12 app/models/refine/filters/criterion.rb
refine-rails-2.11.11 app/models/refine/filters/criterion.rb
refine-rails-2.11.10 app/models/refine/filters/criterion.rb
refine-rails-2.11.9 app/models/refine/filters/criterion.rb
refine-rails-2.11.8 app/models/refine/filters/criterion.rb
refine-rails-2.11.6 app/models/refine/filters/criterion.rb
refine-rails-2.11.5 app/models/refine/filters/criterion.rb
refine-rails-2.11.4 app/models/refine/filters/criterion.rb
refine-rails-2.11.3 app/models/refine/filters/criterion.rb
refine-rails-2.11.2 app/models/refine/filters/criterion.rb
refine-rails-2.11.1 app/models/refine/filters/criterion.rb
refine-rails-2.11.0 app/models/refine/filters/criterion.rb
refine-rails-2.10.1 app/models/refine/filters/criterion.rb
refine-rails-2.10.0 app/models/refine/filters/criterion.rb
refine-rails-2.9.14 app/models/refine/filters/criterion.rb
refine-rails-2.9.13 app/models/refine/filters/criterion.rb
refine-rails-2.9.12 app/models/refine/filters/criterion.rb
refine-rails-2.9.11 app/models/refine/filters/criterion.rb
refine-rails-2.9.10 app/models/refine/filters/criterion.rb