Class: Hipaapotamus::Action

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/hipaapotamus/action.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#source_transaction_stateObject

Returns the value of attribute source_transaction_state.



7
8
9
# File 'lib/hipaapotamus/action.rb', line 7

def source_transaction_state
  @source_transaction_state
end

Class Method Details

.bulk_insert(actions) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/hipaapotamus/action.rb', line 82

def bulk_insert(actions)
  if actions.length > 0
    actions.each do |action|
      raise ActiveRecord::RecordInvalid, 'unable to modify existing actions' unless action.new_record?
      raise ActiveRecord::RecordInvalid, action.errors.full_messages.to_sentence unless action.valid?
    end

    attributeses = actions.map(&:attributes)

    now = DateTime.now
    attributeses.each { |attributes| attributes['created_at'] = now } if self.column_names.include?('created_at')
    attributeses.each { |attributes| attributes['updated_at'] = now } if self.column_names.include?('updated_at')

    uniq_keys = attributeses.map { |attributes| attributes.keys }.flatten(1).uniq.reject { |key| key == primary_key || key == primary_key.to_sym }

    column_names = uniq_keys.map(&:to_s)
    rows = attributeses.map { |attributes| uniq_keys.map { |key| attributes[key] } }

    value_template = "(#{column_names.map{'?'}.join(', ')})"

    value_clauses = rows.map { |values| sanitize_sql_array([value_template, *values]) }
    values_clause = value_clauses.join(', ')

    column_clauses = column_names.map { |column_name| connection.quote_column_name(column_name) }
    columns_clause = "#{connection.quote_column_name(table_name)} (#{column_clauses.join(', ')})"

    insert_statement = "INSERT INTO #{columns_clause} VALUES #{values_clause};"

    connection.execute(insert_statement)
  end
end

Instance Method Details

#agentObject



24
25
26
27
28
29
30
# File 'lib/hipaapotamus/action.rb', line 24

def agent
  if agent_class < Singleton
    agent_class.instance
  else
    agent_class.find(agent_id)
  end
end

#agent=(agent) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/hipaapotamus/action.rb', line 32

def agent=(agent)
  if agent.is_a? Singleton
    self.agent_id = nil
  else
    self.agent_id = agent.id
  end

  self.agent_type = agent.class.name
end

#agent_classObject



20
21
22
# File 'lib/hipaapotamus/action.rb', line 20

def agent_class
  agent_type.try(:constantize)
end

#log_worthy?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/hipaapotamus/action.rb', line 16

def log_worthy?
  persisted? || !transactional? || source_transaction_state.committed?
end

#protectedObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hipaapotamus/action.rb', line 46

def protected
  @protected ||= protected_class.new.tap do |protected|
    if protected_id.present?
      protected.id = protected_id
    end

    if protected_attributes.present?
      protected.assign_attributes protected_attributes
    end

    protected.authorize_access!
  end
end

#protected=(protected) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/hipaapotamus/action.rb', line 60

def protected=(protected)
  self.protected_id = protected.try(:id)
  self.protected_type = protected.try(:class).try(:name)
  self.protected_attributes = protected.try(:attributes)

  @protected = protected
end

#protected_attributesObject



68
69
70
# File 'lib/hipaapotamus/action.rb', line 68

def protected_attributes
  JSON.parse(serialized_protected_attributes) if serialized_protected_attributes.present?
end

#protected_attributes=(protected_attributes) ⇒ Object



72
73
74
# File 'lib/hipaapotamus/action.rb', line 72

def protected_attributes=(protected_attributes)
  self.serialized_protected_attributes = protected_attributes.try(:to_json)
end

#protected_classObject



42
43
44
# File 'lib/hipaapotamus/action.rb', line 42

def protected_class
  protected_type.try(:constantize)
end

#transactional?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/hipaapotamus/action.rb', line 12

def transactional?
  is_transactional
end