Class: EntityEvents::EntityEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/entity_events.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, current_user) ⇒ EntityEvent

Returns a new instance of EntityEvent.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/entity_events.rb', line 26

def initialize(params,current_user)
  @params = params
  @action = action
  @current_user = current_user
  
  actor_method = (@action.to_s+'_actor').to_sym
  @actor = if respond_to?(actor_method)
    @actor_is_user_defined = true
    send actor_method
  else
    default_actor
  end

  target_method = (@action.to_s+'_target').to_sym
  @target = if respond_to?(target_method)
    @target_is_user_defined = true
    send target_method
  else
    default_target
  end

end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



24
25
26
# File 'lib/entity_events.rb', line 24

def action
  @action
end

#actorObject (readonly)

Returns the value of attribute actor.



24
25
26
# File 'lib/entity_events.rb', line 24

def actor
  @actor
end

#actor_is_user_definedObject (readonly)

Returns the value of attribute actor_is_user_defined.



24
25
26
# File 'lib/entity_events.rb', line 24

def actor_is_user_defined
  @actor_is_user_defined
end

#current_userObject (readonly)

Returns the value of attribute current_user.



24
25
26
# File 'lib/entity_events.rb', line 24

def current_user
  @current_user
end

#paramsObject (readonly)

Returns the value of attribute params.



24
25
26
# File 'lib/entity_events.rb', line 24

def params
  @params
end

#targetObject (readonly)

Returns the value of attribute target.



24
25
26
# File 'lib/entity_events.rb', line 24

def target
  @target
end

#target_is_user_definedObject (readonly)

Returns the value of attribute target_is_user_defined.



24
25
26
# File 'lib/entity_events.rb', line 24

def target_is_user_defined
  @target_is_user_defined
end

Instance Method Details

#controllerObject

You can override methods after this line, however it is not nessisary.



77
78
79
# File 'lib/entity_events.rb', line 77

def controller
  params[:controller]
end

#default_actorObject



89
90
91
# File 'lib/entity_events.rb', line 89

def default_actor
  current_user
end

#default_targetObject



93
94
95
96
# File 'lib/entity_events.rb', line 93

def default_target
  id = params["#{params[:controller].to_s.singularize}_id"] || params[:id]
  params[:controller].classify.constantize.find id if id
end

#event_classObject



60
61
62
# File 'lib/entity_events.rb', line 60

def event_class
  self.class.name
end

#flagObject



85
86
87
# File 'lib/entity_events.rb', line 85

def flag
  params[:flag]
end

#recordObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/entity_events.rb', line 49

def record
  Interaction.log({
    actor:       actor,
    target:      target,
    action:      action,
    controller:  controller,
    parameters:  YAML::dump(params),
    flag:        flag
  })
end

#should_record?Boolean

if auto_log == false, then should_record? will dictate is the action is recorded

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
# File 'lib/entity_events.rb', line 65

def should_record?
  action_method = (@action.to_s+'?').to_sym
  if respond_to?(action_method)
    #if <action>? is defined follow that
    send action_method
  else
    #else if actor or target is defined, assume we are to record
    actor_is_user_defined || target_is_user_defined
  end
end