Module: Discerner::Methods::Models::ParameterValue

Included in:
ParameterValue
Defined in:
lib/discerner/methods/models/parameter_value.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/discerner/methods/models/parameter_value.rb', line 5

def self.included(base)
  base.send :include, SoftDelete

  # Associations
  base.send :belongs_to,  :parameter,                       inverse_of: :parameter_values
  base.send :has_many,    :search_parameter_values,         inverse_of: :parameter_value, dependent: :destroy
  base.send :has_one,     :parameter_value_categorization,  inverse_of: :parameter_value, dependent: :destroy
  base.send :has_one,     :parameter_value_category,        :through=> :parameter_value_categorization

  # Scopes
  base.send(:scope, :ordered_by_name, -> { base.order('discerner_parameter_values.name ASC') })
  base.send(:scope, :ordered_by_parameter_and_name, -> { base.order('discerner_parameter_values.parameter_id ASC, discerner_parameter_values.name ASC') })

  #Validations
  base.send :validates, :parameter, presence: true
  base.send :validates, :search_value, length: { maximum: 1000 }, uniqueness: {scope: :parameter_id, message: "for parameter value has already been taken"}
  base.send :validates, :name, presence: true, length: { maximum: 1000 }
  base.send :validate,  :parameter_category_belongs_to_parameter

  # Hooks
  base.send :after_commit, :create_search_parameter_values, on: :create
  base.send :after_commit, :update_search_parameter_values, on: :update, if: Proc.new { |record| record.previous_changes.include?('deleted_at') }
  base.send :scope, :categorized, -> {base.joins(:parameter_value_category)}
  base.send :scope, :uncategorized, -> {base.includes(:parameter_value_category).where(discerner_parameter_value_categories: {name: nil})}
end

Instance Method Details

#display_nameObject



44
45
46
47
48
49
50
# File 'lib/discerner/methods/models/parameter_value.rb', line 44

def display_name
  if parameter_value_category
    "#{parameter_value_category.name} - #{name} "
  else
    name
  end
end

#initialize(*args) ⇒ Object

Instance Methods



32
33
34
# File 'lib/discerner/methods/models/parameter_value.rb', line 32

def initialize(*args)
  super(*args)
end

#used_in_search?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
# File 'lib/discerner/methods/models/parameter_value.rb', line 36

def used_in_search?
  if parameter.parameter_type.name == 'list'
    search_parameter_values.chosen.any?
  else
    search_parameter_values.any?
  end
end