Module: Hobo::Model::Lifecycles::Actions

Included in:
Creator, State, Transition
Defined in:
lib/hobo/model/lifecycles/actions.rb

Instance Method Summary collapse

Instance Method Details

#acting_user_is?(who, record) ⇒ Boolean

Returns:



52
53
54
# File 'lib/hobo/model/lifecycles/actions.rb', line 52

def acting_user_is?(who, record)
  publishable_by(record.acting_user, who, record)
end

#apply_user_becomes!(record) ⇒ Object



122
123
124
125
126
# File 'lib/hobo/model/lifecycles/actions.rb', line 122

def apply_user_becomes!(record)
  if (assoc = options[:user_becomes])
    record.send("#{assoc}=", record.acting_user)
  end
end

#available_toObject



107
108
109
# File 'lib/hobo/model/lifecycles/actions.rb', line 107

def available_to
  options[:available_to]
end

#available_to_acting_user?(record) ⇒ Boolean

Returns:



13
14
15
16
# File 'lib/hobo/model/lifecycles/actions.rb', line 13

def available_to_acting_user?(record)
  return true if available_to.nil? # available_to not specified (these steps are not published)
  acting_user_is?(available_to, record)
end

#can_run?(record) ⇒ Boolean

Returns:



102
103
104
# File 'lib/hobo/model/lifecycles/actions.rb', line 102

def can_run?(record)
  available_to_acting_user?(record) && guard_ok?(record) && record.lifecycle.invariants_satisfied?
end

#fire_event(record, event) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/hobo/model/lifecycles/actions.rb', line 72

def fire_event(record, event)
  if event
    if event.arity == 1
      event.call(record)
    else
      record.instance_eval(&event)
    end
  end
end

#get_state(record, state) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/hobo/model/lifecycles/actions.rb', line 128

def get_state(record, state)
  case state
  when Proc
    if state.arity == 1
      state.call(record)
    else
      record.instance_eval(&state)
    end
  when String
    eval(state, record.instance_eval { binding })
  else
    state
  end
end

#guard_ok?(record) ⇒ Boolean

Returns:



83
84
85
86
87
88
89
90
91
92
# File 'lib/hobo/model/lifecycles/actions.rb', line 83

def guard_ok?(record)
  if options[:if]
    raise ArgumentError, "do not provide both :if and :unless to lifecycle steps" if options[:unless]
    run_hook(record, options[:if])
  elsif options[:unless]
    !run_hook(record, options[:unless])
  else
    true
  end
end

#prepare!(record, attributes) ⇒ Object



95
96
97
98
99
# File 'lib/hobo/model/lifecycles/actions.rb', line 95

def prepare!(record, attributes)
  record.attributes = extract_attributes(attributes) if attributes
  record.lifecycle.generate_key if options[:new_key]
  apply_user_becomes!(record)
end

#publishable?Boolean

Returns:



112
113
114
# File 'lib/hobo/model/lifecycles/actions.rb', line 112

def publishable?
  available_to
end

#publishable_by(user, who, record) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/hobo/model/lifecycles/actions.rb', line 18

def publishable_by(user, who, record)
  case who
  when :all
    true

  when :key_holder
    record.lifecycle.valid_key?

  when :self
    record == user

  when Array
    # recursively apply the same test to every item in the array
    who.detect { |w| publishable_by(user, w, record) }

  else
    refl = record.class.reflections[who.to_s]
    if refl && refl.macro == :has_many
      record.send(who).include?(user)
    elsif refl && refl.macro == :belongs_to
      record.send("#{who}_is?", user)
    else
      value = run_hook(record, who)
      if value.is_a?(Class)
        user.is_a?(value)
      elsif value.respond_to?(:include?)
        value.include?(user)
      else
        value == user
      end
    end
  end
end

#routable_for?(subsite) ⇒ Boolean

Returns:



117
118
119
# File 'lib/hobo/model/lifecycles/actions.rb', line 117

def routable_for?(subsite)
  publishable? && options[:subsite] == subsite
end

#run_hook(record, hook, *args) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hobo/model/lifecycles/actions.rb', line 56

def run_hook(record, hook, *args)
  case hook
  when Symbol
    record.send(hook, *args)
  when String
    __top_level_eval__(record, hook)
  when Proc
    if hook.arity == 1
      hook.call(record)
    else
      record.instance_eval(&hook)
    end
  end
end