Class: Accountability::Offerable::Scope

Inherits:
Object
  • Object
show all
Defined in:
app/models/accountability/offerable/scope.rb

Overview

Scope objects represent an offerable’s scoping options as defined by ‘offer.add_scope` The plan is to replace the Offerable#scopes hash attribute with an array of scope objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_class:, category:, attribute:, title: nil, options: :auto) ⇒ Scope

Returns a new instance of Scope.



8
9
10
11
12
13
14
# File 'app/models/accountability/offerable/scope.rb', line 8

def initialize(source_class:, category:, attribute:, title: nil, options: :auto)
  @source_class = source_class
  @category = category
  @attribute = attribute
  @title = title.presence || attribute.to_s.titleize
  @options = options
end

Instance Attribute Details

#attributeObject

Returns the value of attribute attribute.



5
6
7
# File 'app/models/accountability/offerable/scope.rb', line 5

def attribute
  @attribute
end

#categoryObject

Returns the value of attribute category.



5
6
7
# File 'app/models/accountability/offerable/scope.rb', line 5

def category
  @category
end

#optionsObject

Returns an array of values to choose from when defining the scope for a new product. The product’s ‘source_scope` column stores a hash mapping each scopes’ @attribute with the selected options.

For example, if an offerable called ‘Bucket’ has a scoped :color attribute with options %w[red green black grey], a “colored bucket” product can be created with a ‘source_scope` of `{ color: %w[red green] }`. Buckets available for purchase would be automatically found by querying `Bucket.where(color: %w[red green])`.

When an offerable’s scope has no options, it defaults to ‘:auto` and is generated automatically based on the attribute type when `#options` is called instead.

String  - Returns a unique array of values plucked from the attribute's column
Enum    - Returns am array containing each valid enum value
Boolean - Returns [true, false]

Any other attribute type will return an empty array.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/accountability/offerable/scope.rb', line 29

def options
  return @options unless @options == :auto

  @options = case attribute_type
             when ActiveModel::Type::String
               source_class.distinct.pluck(attribute)
             when ActiveRecord::Enum::EnumType
               enums = source_class.defined_enums.with_indifferent_access
               enums[attribute].keys
             when ActiveModel::Type::Boolean
               [true, false]
             else
               []
             end
end

#source_classObject

Returns the value of attribute source_class.



5
6
7
# File 'app/models/accountability/offerable/scope.rb', line 5

def source_class
  @source_class
end

#titleObject

Returns the value of attribute title.



5
6
7
# File 'app/models/accountability/offerable/scope.rb', line 5

def title
  @title
end

Instance Method Details

#attribute_typeObject



45
46
47
# File 'app/models/accountability/offerable/scope.rb', line 45

def attribute_type
  source_class.type_for_attribute(attribute)
end