Class: Card::Act

Inherits:
ApplicationRecord show all
Defined in:
mod/history/lib/card/act.rb,
mod/history/lib/card/act/act_renderer.rb,
mod/history/lib/card/act/act_renderer/absolute_act_renderer.rb,
mod/history/lib/card/act/act_renderer/relative_act_renderer.rb

Overview

An "act" is a group of recorded actions on cards. Together, acts, actions, and changes comprise a comprehensive card history tracking system.

For example, if a given web form submissions updates the contents of three cards, then the submission will result in the recording of three actions, each of which is tied to one act.

Each act records:

  • the actor_id (an id associated with the account responsible)
  • the card_id of the act's primary card
  • acted_at, a timestamp of the action
  • the ip_address of the actor where applicable.

Defined Under Namespace

Classes: ActRenderer

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_viewableArray of Actions

all actions that current user has permission to view

Returns:

  • (Array of Actions)


52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'mod/history/lib/card/act.rb', line 52

def all_viewable
  card_join = "JOIN cards ON cards.id = card_actions.card_id"
  action_conditions = [
    "card_acts.id = card_act_id",
    "card_actions.card_id is not null",
    "draft is not true",
    Query::SqlStatement.new.permission_conditions("cards")
  ].compact.join " AND "

  viewable_actions =
    Action.joins(card_join).where(action_conditions).to_sql
  where("card_id is not null AND EXISTS (#{viewable_actions})")
end

.delete_actionlessObject

remove all acts that have no action. (janitorial)



30
31
32
33
34
35
36
# File 'mod/history/lib/card/act.rb', line 30

def delete_actionless
  joins(
    "LEFT JOIN card_actions ON card_acts.id = card_act_id"
  ).where(
    "card_actions.id is null"
  ).delete_all
end

.delete_cardlessObject

remove all acts that have no card. (janitorial)



24
25
26
27
# File 'mod/history/lib/card/act.rb', line 24

def delete_cardless
  left_join = "LEFT JOIN cards ON card_acts.card_id = cards.id"
  joins(left_join).where("cards.id IS NULL").delete_all
end

.find_all_with_actions_on(card_ids, args = {}) ⇒ Array of Actions

all actions on a set of card ids

Parameters:

  • card_ids (Array of Integers)
  • args (Hash) (defaults to: {})

    with_drafts: [true, false]

Returns:

  • (Array of Actions)


43
44
45
46
47
48
# File 'mod/history/lib/card/act.rb', line 43

def find_all_with_actions_on card_ids, args={}
  sql = "card_actions.card_id IN (:card_ids) AND ( (draft is not true) "
  sql << (args[:with_drafts] ? "OR actor_id = :current_user_id)" : ")")
  vars = { card_ids: card_ids, current_user_id: Card::Auth.current_id }
  joins(:actions).where(sql, vars).distinct.order(:id).reverse_order
end

Instance Method Details

#action_on(card_id) ⇒ Card::Action

act's action on the card in question

Parameters:

  • card_id (Integer)

Returns:



78
79
80
# File 'mod/history/lib/card/act.rb', line 78

def action_on card_id
  actions.where("card_id = #{card_id} and draft is not true").first
end

#actions_affecting(card) ⇒ Array of Actions

act's actions on either the card itself or another card that includes it

Parameters:

Returns:

  • (Array of Actions)


101
102
103
104
105
106
# File 'mod/history/lib/card/act.rb', line 101

def actions_affecting card
  actions.select do |action|
    (card.id == action.card_id) ||
      card.included_card_ids.include?(action.card_id)
  end
end

#cardCard

the act's primary card

Returns:



69
70
71
72
73
# File 'mod/history/lib/card/act.rb', line 69

def card
  res = Card.fetch card_id, look_in_trash: true, skip_modules: true
  return res unless res && res.type_id.in?([FileID, ImageID])
  res.include_set_modules
end

#draft?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'mod/history/lib/card/act.rb', line 88

def draft?
  main_action.draft
end

#elapsed_timeString

time (in words) since act took place

Returns:

  • (String)


94
95
96
# File 'mod/history/lib/card/act.rb', line 94

def elapsed_time
  DateTime.new(acted_at).distance_of_time_in_words_to_now
end

#main_actionCard::Action

act's action on primary card if it exists. otherwise act's first action

Returns:



84
85
86
# File 'mod/history/lib/card/act.rb', line 84

def main_action
  action_on(card_id) || actions.first
end