Class: Action

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
OhNoes::Destroy
Defined in:
app/models/action.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OhNoes::Destroy

#destroy, #destroyable?

Class Method Details

.create_of_type(type) ⇒ Object

Action types: give, go, do, get, join, hear



27
28
29
30
31
32
33
34
35
# File 'app/models/action.rb', line 27

def self.create_of_type(type)
  case type
    when "hear" then HearAction.new
    when "say" then SayAction.new
    when "go" then GoAction.new
    when "give" then GiveAction.new
    when "do" then DoAction.new
  end
end

.for_organization(organization) ⇒ Object



17
18
19
20
21
22
# File 'app/models/action.rb', line 17

def self.for_organization(organization)
  #Skipping whitelisting because we're playing on our home field
  Kernel.const_get(self.name.camelize).new({:occurred_at  => DateTime.now.in_time_zone(organization.time_zone), 
                                            :organization => organization},
                                            :without_protection => true)
end

.from_open_struct(action_struct) ⇒ Object



120
121
122
123
124
125
126
# File 'app/models/action.rb', line 120

def self.from_open_struct(action_struct)
  action = Kernel.const_get(action_struct.type).new
  Kernel.const_get(action_struct.type).column_names.each do |col|
    action.send("#{col}=", action_struct.send(col)) unless action_struct.send(col).nil?
  end
  action    
end

.recent(organization) ⇒ Object

This returnes an ARel, so you can chain



93
94
95
# File 'app/models/action.rb', line 93

def self.recent(organization)
  Action.includes(:person, :subject).where(:organization_id => organization).order('occurred_at DESC')
end

.subtypesObject



97
98
99
# File 'app/models/action.rb', line 97

def self.subtypes
  []
end

.subtypes_by_typeObject



37
38
39
40
41
42
43
44
45
46
# File 'app/models/action.rb', line 37

def self.subtypes_by_type
  {
    "hear" => HearAction.subtypes,
    "say" => SayAction.subtypes,
    "go" => GoAction.subtypes,
    "give" => GiveAction.subtypes,
    "do" => DoAction.subtypes,
    "get" => GetAction.subtypes
  }
end

Instance Method Details

#default_starred_to_falseObject



101
102
103
104
# File 'app/models/action.rb', line 101

def default_starred_to_false
  self.starred = false if self.starred.blank?
  true
end

#editable?Boolean

destroyable? is mixed-in from ohnoes.rb

Returns:

  • (Boolean)


49
50
51
# File 'app/models/action.rb', line 49

def editable?
  true
end

#full_detailsObject



88
89
90
# File 'app/models/action.rb', line 88

def full_details
  details
end

#quipObject



80
81
82
# File 'app/models/action.rb', line 80

def quip
  verb
end

#sentenceObject



84
85
86
# File 'app/models/action.rb', line 84

def sentence
  verb + " " + details
end

#set_creator(user) ⇒ Object



67
68
69
70
# File 'app/models/action.rb', line 67

def set_creator(user)
  self.creator_id = user.id
  self.organization_id = user.current_organization.id
end

#set_params(params, person) ⇒ Object

set_params and set_creator need to be wrapped up into an initialize method



56
57
58
59
60
61
62
63
64
65
# File 'app/models/action.rb', line 56

def set_params(params, person)
  params ||= {}

  self.occurred_at = ActiveSupport::TimeZone.create(Organization.find(self.organization_id).time_zone).parse(params[:occurred_at]) if params[:occurred_at].present?
  self.subtype = params[:subtype]
  self.details = params[:details]

  self.person = person
  self.subject = person
end

#to_open_structObject

This exists solely so that DJ can serialize an unsaved action so that ActionJob can run There’s nothing inherent to Action in this method so it looks like a good candidate to move into a Module. But, I’m not thrilled with it and don’t want it used willy-nilly Perhaps in an DelayedJob::Unsaved::Serializable Module or something.



112
113
114
115
116
117
118
# File 'app/models/action.rb', line 112

def to_open_struct
  action_struct = OpenStruct.new
  self.class.column_names.each do |col|
    action_struct.send("#{col}=", self.send(col))
  end
  action_struct
end

#unstarred?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'app/models/action.rb', line 72

def unstarred?
  !starred?
end

#verbObject



76
77
78
# File 'app/models/action.rb', line 76

def verb
  verb
end