Class: Page

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

Instance Method Summary collapse

Instance Method Details

#after_initializeObject



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

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

#before_saveObject



79
80
81
82
83
84
85
86
# File 'app/models/page.rb', line 79

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



72
73
74
75
76
77
# File 'app/models/page.rb', line 72

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

#editable_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
140
141
# File 'app/models/page.rb', line 137

def editable_by?(user)
  user == original_author ||
  is_open_to_all? ||
  editors.split.include?(user.)
end

#editorsObject



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

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

#is_open_to_all?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'app/models/page.rb', line 95

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

#nameObject



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

def name; name_before_type_cast; end

#name_before_type_castObject



89
90
91
92
93
# File 'app/models/page.rb', line 89

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

#original_authorObject



34
35
36
# File 'app/models/page.rb', line 34

def original_author
  oldest(:last_editor)
end

#positionObject



59
60
61
# File 'app/models/page.rb', line 59

def position
  nil
end

#reported_byObject



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

def reported_by
  self.kind.pluralize
end

#revise(author, time, attrs) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/page.rb', line 99

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, rev.done = attrs[:title], attrs[:text], attrs[:done]
  update_references(rev.text) if rev.text
  self.revisions << rev
  
  save
  self
end

#textObject



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

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

#titleObject



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

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

#to_headlineObject



128
129
130
131
132
133
134
135
# File 'app/models/page.rb', line 128

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