Class: Accountability::Offerable

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

Overview

TODO: Reconsider using names as keys, maybe just arrays?

Defined Under Namespace

Classes: Scope

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(category, tenant: :default, trait: nil, class_name:) ⇒ Offerable

Returns a new instance of Offerable.



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

def initialize(category, tenant: :default, trait: nil, class_name:)
  @category = category
  @tenant = tenant
  @class_name = class_name
  @trait = trait
  @scopes = {}
  @properties = {}
  @callbacks = {}
  @whitelist = :all
end

Instance Attribute Details

#callbacksObject

Returns the value of attribute callbacks.



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

def callbacks
  @callbacks
end

#categoryObject

Returns the value of attribute category.



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

def category
  @category
end

#class_nameObject

Returns the value of attribute class_name.



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

def class_name
  @class_name
end

#propertiesObject

Returns the value of attribute properties.



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

def properties
  @properties
end

#scopesObject

Returns the value of attribute scopes.



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

def scopes
  @scopes
end

#tenantObject

Returns the value of attribute tenant.



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

def tenant
  @tenant
end

#traitObject

Returns the value of attribute trait.



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

def trait
  @trait
end

#whitelistObject

Returns the value of attribute whitelist.



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

def whitelist
  @whitelist
end

Class Method Details

.add(category, tenant: :default, trait: nil, class_name:) ⇒ Object



18
19
20
21
22
23
24
# File 'app/models/accountability/offerable.rb', line 18

def self.add(category, tenant: :default, trait: nil, class_name:)
  category = category.to_s.underscore.downcase.to_sym
  offerable = new(category, tenant: tenant, trait: trait, class_name: class_name)
  collection[category] = offerable

  offerable
end

Instance Method Details

#add_callback(method_name, **options) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/accountability/offerable.rb', line 72

def add_callback(method_name, **options)
  tense, event = options.slice(:before, :after).flatten
  trigger = "#{tense}_#{event}".to_sym

  params = if options[:with_options].present?
             options[:with_options].to_h { |option| [option, option] }
           else
             [*options[:with]]
           end

  callbacks[trigger] ||= []
  callbacks[trigger] << { method_name: method_name, params: params }

  self
end

#add_properties(*names) ⇒ Object

Used for setting multiple properties in a single line. Column names will be used as titles, and property order retained. ‘offer.add_properties :asset_tag, :room_number, :color`



64
65
66
67
68
69
70
# File 'app/models/accountability/offerable.rb', line 64

def add_properties(*names)
  names.each do |name|
    add_property(name)
  end

  self
end

#add_property(name, title: name, position: nil) ⇒ Object

Used to differentiate records within a product’s scope/inventory. For example, consider a co-location that allows customers to choose their own cabinet:

`offer.add_property :cabinet_location_column, title: 'Asset Tag'`

Use the ‘position` column if it is important that the properties display in a specific order.

CONSIDER: Add support for property-specific pricing



54
55
56
57
58
59
# File 'app/models/accountability/offerable.rb', line 54

def add_property(name, title: name, position: nil)
  position = properties.size.next if position.nil?
  properties[name] = { title: title.to_s, position: position }

  self
end

#add_scope(name, title: name, options: :auto) ⇒ Object

Used when creating a product to define queries identifying saleable records from the host table. This can be used to de-scope sold inventory, but it is recommended to define a whitelist for that instead.

CONSIDER: Add a parameter for indicating optional scopes



30
31
32
33
34
# File 'app/models/accountability/offerable.rb', line 30

def add_scope(name, title: name, options: :auto)
  scopes[name] = { title: title.to_s, options: options, category: category }

  self
end

#inventory_whitelist(whitelist_scope) ⇒ Object

Used for limiting the product’s full inventory scope to a subset. Non-whitelisted records will be treated the same as a private product’s inventory.

The method takes the name of a pre-defined ActiveRecord scope as an argument. If no whitelist is specified, :all will be used instead.



41
42
43
44
45
# File 'app/models/accountability/offerable.rb', line 41

def inventory_whitelist(whitelist_scope)
  self.whitelist = whitelist_scope.to_sym

  self
end

#trait?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'app/models/accountability/offerable.rb', line 88

def trait?
  trait.present?
end