Class: Workarea::ProductRule

Inherits:
Object
  • Object
show all
Includes:
ApplicationDocument, Releasable
Defined in:
app/models/workarea/product_rule.rb

Constant Summary collapse

OPERATORS =
%w(
  greater_than
  greater_than_or_equal
  equals
  not_equal
  less_than
  less_than_or_equal
)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Releasable

#changesets_with_children, #destroy, #in_release, #release_changes, #release_originals, #save_changeset, #skip_changeset, #without_release

Methods included from Segmentable

#active?, #active_segment_ids_with_children, #segmented?, #segments

Methods included from Release::Activation

#activate_with?, #create_activation_changeset, #save_activate_with, #was_active?

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

add_worker, assert_valid_config!, async, caching_classes?, disable, enable, inline, #run_callbacks, workers, workers_list

Methods included from Mongoid::Document

#embedded_children

Class Method Details

.usableObject



26
27
28
# File 'app/models/workarea/product_rule.rb', line 26

def self.usable
  scoped.select(&:active?).select(&:valid?)
end

Instance Method Details

#category?Boolean

Determines if the rule is a for a category.

Returns:

  • (Boolean)


113
114
115
# File 'app/models/workarea/product_rule.rb', line 113

def category?
  slug == :category
end

#comparison?Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
# File 'app/models/workarea/product_rule.rb', line 100

def comparison?
  operator.in?([
    'greater_than',
    'greater_than_or_equal',
    'less_than',
    'less_than_or_equal'
  ])
end

#created_at?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'app/models/workarea/product_rule.rb', line 80

def created_at?
  slug == :created_at
end

#equality?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'app/models/workarea/product_rule.rb', line 84

def equality?
  operator == 'equals'
end

#false?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'app/models/workarea/product_rule.rb', line 96

def false?
  value.downcase == 'false'
end

#fieldString

The field in Elasticsearch this rule checks against

Returns:



42
43
44
45
# File 'app/models/workarea/product_rule.rb', line 42

def field
  return if name.blank?
  Workarea.config.product_rule_fields[slug] || "facets.#{name.systemize}"
end

#inequality?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'app/models/workarea/product_rule.rb', line 88

def inequality?
  operator == 'not_equal'
end

#inventory?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'app/models/workarea/product_rule.rb', line 76

def inventory?
  slug == :inventory
end

#product_exclusion?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'app/models/workarea/product_rule.rb', line 117

def product_exclusion?
  slug == :excluded_products
end

#sale?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'app/models/workarea/product_rule.rb', line 68

def sale?
  slug == :on_sale
end

#search?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'app/models/workarea/product_rule.rb', line 72

def search?
  slug == :search
end

#slugSymbol

The identifier for which admin partials to render for this rule.

Returns:

  • (Symbol)


34
35
36
# File 'app/models/workarea/product_rule.rb', line 34

def slug
  name.systemize.to_sym if name.present?
end

#termsArray<String>

Used in query-building for Elasticsearch

Returns:



125
126
127
# File 'app/models/workarea/product_rule.rb', line 125

def terms
  value.to_s.split(',').map(&:strip).reject(&:blank?)
end

#true?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'app/models/workarea/product_rule.rb', line 92

def true?
  value.downcase == 'true'
end

#valueComparable

Returns the value that the rule uses to compare.

Returns:

  • (Comparable)


53
54
55
56
# File 'app/models/workarea/product_rule.rb', line 53

def value
  return Time.zone.parse(value_field) rescue value_field if created_at?
  value_field
end

#value=(val) ⇒ Object

This is HACK for the fact that we want a CSV string in value, but our current solution for submitting category IDs submits an array. :(



60
61
62
63
64
65
66
# File 'app/models/workarea/product_rule.rb', line 60

def value=(val)
  if val.is_a?(Array)
    super(val.join(','))
  else
    super(val)
  end
end

#value_fieldObject



47
# File 'app/models/workarea/product_rule.rb', line 47

alias_method :value_field, :value