module Hobo::Model::Lifecycles::Actions

Public Instance Methods

acting_user_is?(who, record) click to toggle source
# 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) click to toggle source
# 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_to() click to toggle source
# File lib/hobo/model/lifecycles/actions.rb, line 107
def available_to
  options[:available_to]
end
available_to_acting_user?(record) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/hobo/model/lifecycles/actions.rb, line 112
def publishable?
  available_to
end
publishable_by(user, who, record) click to toggle source
# 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) click to toggle source
# File lib/hobo/model/lifecycles/actions.rb, line 117
def routable_for?(subsite)
  publishable? && options[:subsite] == subsite
end
run_hook(record, hook, *args) click to toggle source
# 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