Class: Kit

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActiveRecord::Transitions, Ext::Integrations::Kit
Defined in:
app/models/kit.rb

Defined Under Namespace

Classes: DuplicateError

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ext::Integrations::Kit

#record_activation, #record_approval

Class Method Details

.activate(options) ⇒ Object



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

def self.activate(options)
  activation_requirements[:unless] << options.delete(:unless) if options.has_key?(:unless)
  activation_requirements[:if] << options.delete(:if) if options.has_key?(:if)
end

.activation_requirementsObject



57
58
59
# File 'app/models/kit.rb', line 57

def self.activation_requirements
  @requirements ||= Hash.new { |h,k|  h[k] = [] }
end

.acts_as_kit(options = {}, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/kit.rb', line 14

def self.acts_as_kit(options = {}, &block)
  self.requires_approval = options.delete(:with_approval) || false
  self.restricted_to_admins = options.delete(:admin_only) || false

  state_machine do
    state :fresh
    state :pending, :enter => :on_pending
    state :activated, :enter => :on_activation
    state :cancelled

    event(:activate, :success => :record_activation)   { transitions :from => [:fresh, :pending], :to => :activated, :guard => :activatable? }
    event(:approve, :success => :record_approval)    { transitions :from => :pending, :to => :activated, :guard => :approvable? }
    event(:admin_approve, :success => :record_approval)    { transitions :from => :pending, :to => :activated, :guard => :admin_approvable? }
    event(:cancel)     { transitions :from => [:activated, :pending, :rejected ], :to => :cancelled }
    event(:reactivate) { transitions :from => :cancelled, :to => :activated, :guard => :activatable? }
    event(:activate_without_pending) { transitions :from => [:fresh, :pending, :cancelled], :to => :activated }
  end

  if self.requires_approval
    state_machine do
      event(:submit_for_approval) { transitions :from => :fresh, :to => :pending }
    end
  end

  class_eval(&block)
  self
end

.admin_approval_requirementsObject



65
66
67
# File 'app/models/kit.rb', line 65

def self.admin_approval_requirements
  @admin_approval_requirements ||= Hash.new { |h,k|  h[k] = [] }
end

.admin_approve(options) ⇒ Object



52
53
54
55
# File 'app/models/kit.rb', line 52

def self.admin_approve(options)
  admin_approval_requirements[:unless] << options.delete(:unless) if options.has_key?(:unless)
  admin_approval_requirements[:if] << options.delete(:if) if options.has_key?(:if)
end

.approval_requirementsObject



61
62
63
# File 'app/models/kit.rb', line 61

def self.approval_requirements
  @approval_requirements ||= Hash.new { |h,k|  h[k] = [] }
end

.approve(options) ⇒ Object



47
48
49
50
# File 'app/models/kit.rb', line 47

def self.approve(options)
  approval_requirements[:unless] << options.delete(:unless) if options.has_key?(:unless)
  approval_requirements[:if] << options.delete(:if) if options.has_key?(:if)
end

.mailchimpObject



85
86
87
# File 'app/models/kit.rb', line 85

def self.mailchimp
  find_by_type("MailchimpKit")
end

.pad_with_new_kits(kits = []) ⇒ Object



77
78
79
80
81
82
83
# File 'app/models/kit.rb', line 77

def self.pad_with_new_kits(kits = [])
  types = kits.collect(&:type)
  alternatives = kits.collect(&:alternatives).flatten.uniq

  padding = subklasses.reject{ |klass| klass.to_s == "SponsoredDonationKit" }.reject{ |klass| (types.include? klass.to_s) or (alternatives.include? klass) }.collect(&:new)
  kits + padding
end

.subklassesObject



73
74
75
# File 'app/models/kit.rb', line 73

def self.subklasses
  @subklasses ||= [ TicketingKit, RegularDonationKit, SponsoredDonationKit, ResellerKit, MailchimpKit, MembershipKit, PassesKit, ScannableTicketsKit, RelationshipsKit ].freeze
end

.visibleObject



10
11
12
# File 'app/models/kit.rb', line 10

def self.visible
  where(Kit.arel_table[:state].eq("activated").or(Kit.arel_table[:state].eq('pending')))
end

.when_active(&block) ⇒ Object



69
70
71
# File 'app/models/kit.rb', line 69

def self.when_active(&block)
  self.ability_proc = Proc.new(&block)
end

Instance Method Details

#abilitiesObject



89
90
91
# File 'app/models/kit.rb', line 89

def abilities
  activated? ? self.class.ability_proc : Proc.new {}
end

#activatable?Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/kit.rb', line 105

def activatable?
  return false if organization.nil?

  if needs_approval?
    check_requirements
    submit_for_approval!
    return false
  end

  check_requirements
end

#admin_approvable?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'app/models/kit.rb', line 121

def admin_approvable?
  check_admin_approval
end

#alternativesObject



97
98
99
# File 'app/models/kit.rb', line 97

def alternatives
  []
end

#approvable?Boolean

Returns:

  • (Boolean)


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

def approvable?
  check_approval
end

#has_alternatives?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'app/models/kit.rb', line 93

def has_alternatives?
  alternatives.any?
end

#requirements_met?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'app/models/kit.rb', line 101

def requirements_met?
  check_requirements
end