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(request, 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
# File 'lib/entity_events.rb', line 26

def initialize(request, current_user)
  @request = request
  @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

#requestObject (readonly)

Returns the value of attribute request.



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

def request
  @request
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.



75
76
77
# File 'lib/entity_events.rb', line 75

def controller
  request.filtered_parameters[:controller]
end

#default_actorObject



87
88
89
# File 'lib/entity_events.rb', line 87

def default_actor
  current_user
end

#default_targetObject



91
92
93
94
# File 'lib/entity_events.rb', line 91

def default_target
  id = request.filtered_parameters["#{request.filtered_parameters[:controller].to_s.singularize}_id"] || request.filtered_parameters[:id]
  request.filtered_parameters[:controller].classify.split(':').last.constantize.find id if id
end

#event_classObject



58
59
60
# File 'lib/entity_events.rb', line 58

def event_class
  self.class.name
end

#flagObject



83
84
85
# File 'lib/entity_events.rb', line 83

def flag
  request.filtered_parameters[:flag]
end

#recordObject



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

def record
  Interaction.log({  actor:       actor,
                     target:      target,
                     action:      action,
                     controller:  controller,
                     flag:        flag,
                     request_ip:  request.remote_ip
                  })
end

#should_record?Boolean

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

Returns:

  • (Boolean)


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

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