Class: Page

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/page.rb

Instance Method Summary collapse

Instance Method Details

#after_initializeObject



55
56
57
58
# File 'app/models/page.rb', line 55

def after_initialize
  default('', :name)
  default('common', :kind)
end

#before_saveObject



67
68
69
70
71
72
73
74
# File 'app/models/page.rb', line 67

def before_save
  if title == PLACE_HOLDER_TITLE.t
    revisions.last.title = title_from_kind  
  end
  
  write_attribute(:modified_at, self.modified_at)
  write_attribute(:name, self.name)
end

#default(value, *attrs) ⇒ Object



60
61
62
63
64
65
# File 'app/models/page.rb', line 60

def default(value, *attrs)
  attrs.each do |attr|
    cur = send(attr)
    send("#{attr}=".to_sym, value) if cur.nil? || cur.empty?
  end
end

#editorsObject



42
43
44
# File 'app/models/page.rb', line 42

def editors
  most_recent(:editors) || ''
end

#is_open_to_all?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'app/models/page.rb', line 83

def is_open_to_all?
  0 == editors.strip.size
end

#nameObject



76
# File 'app/models/page.rb', line 76

def name; name_before_type_cast; end

#name_before_type_castObject



77
78
79
80
81
# File 'app/models/page.rb', line 77

def name_before_type_cast
  result = read_attribute(:name)
  return name_from_title if result.nil? || result.empty?
  return result
end

#original_authorObject



26
27
28
# File 'app/models/page.rb', line 26

def original_author
  oldest(:last_editor)
end

#reported_byObject



51
52
53
# File 'app/models/page.rb', line 51

def reported_by
  self.kind.pluralize
end

#revise(author, time, attrs) ⇒ Object



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
113
# File 'app/models/page.rb', line 87

def revise(author, time, attrs)
  #TODO ugly! ugly! ugly!
  if !attrs[:happens_at] && attrs['happens_at(1i)'] && attrs['happens_at(2i)'] && attrs['happens_at(3i)'] 
    attrs[:happens_at] =
      Date.new(attrs['happens_at(1i)'].to_i, attrs['happens_at(2i)'].to_i,
               attrs['happens_at(3i)'].to_i)
  end
  unless revisions.size == 0 || original_author 
    revisions.first.last_editor = author 
    revisions.first.save
  end
  rev = Revision.new(:last_editor => author, :modified_at => time,
                     :position => self.revisions.size + 1)
  rev.editors = if author.can_change_editors?(self)
    attrs[:editors]
  else
    revisions.last.editors
  end
  self.kind = attrs[:kind] if attrs[:kind]
  self.happens_at = attrs[:happens_at] if attrs[:happens_at]
  rev.kind, rev.happens_at = self.kind, self.happens_at
  rev.title, rev.text = attrs[:title], attrs[:text]
  self.revisions << rev
  
  save
  self
end

#textObject



46
47
48
49
# File 'app/models/page.rb', line 46

def text
  most_recent(:text) ||
    ('MainPage' == self.name ? CONGRATS_TEXT : WIKI_NOT_FOUND_TEXT)  
end

#titleObject



36
37
38
39
40
# File 'app/models/page.rb', line 36

def title
  result = most_recent(:title)
  return title_from_name || PLACE_HOLDER_TITLE.t if result.nil? || result.empty?
  return result
end

#to_headlineObject



115
116
117
118
119
120
121
122
# File 'app/models/page.rb', line 115

def to_headline
  #TODO (for 0.7): headlines and pages should _really_ be the same thing
  #                reporters should write ordinary wiki pages
  Headline.new(:rid => name,
               :author => last_editor ? last_editor. : DEFAULT_AUTHOR,
               :happened_at => (kind == 'event' ? happens_at.to_t : modified_at) || DEFAULT_TIME,
               :description => inject_title_into_text)
end