aboutsummaryrefslogtreecommitdiff
blob: 0ce60ea7dd30ee27b40cbafa9997b4a5e51261af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class AgendaItem < ActiveRecord::Base

  hobo_model # Don't put anything above this

  fields do
    title      :string
    discussion :string
    body       :text
    rejected   :boolean, :default => false
    timelimits :text, :null => false, :default => ''
    timestamps
  end

  belongs_to :user, :creator => true
  belongs_to :agenda
  has_many   :voting_options

  validate :timelimits_entered_properly

  # --- Permissions --- #
  def create_permitted?
    return false if acting_user.guest?
    return false if user != acting_user
    true
  end

  def update_permitted?
    return false if agenda._?.state == 'old'
    return false if user_changed?
    return true if acting_user.council_member?
    return true if acting_user.administrator?
    return false unless agenda.nil?
    return true if acting_user == user
    false
  end

  def destroy_permitted?
    acting_user.administrator?
  end

  def view_permitted?(field)
    true
  end

  # Not deduced properly
  def edit_permitted?(field)
    return false if field == :rejected && !agenda.nil?
    return false if field == :agenda && rejected?
    return false if agenda._?.state == 'old'
    return false if field == :user
    return true if acting_user.administrator?
    return true if acting_user.council_member?
    return false unless agenda.nil?
    return acting_user == user if [nil, :title, :discussion, :body].include?(field)
  end

  protected
    def timelimits_entered_properly
      regexp = /^\d+:\d+( .*)?$/
      for line in timelimits.split("\n")
        unless line.match regexp
          errors.add(:timelimits, "Line '#{line}' doensn't match '<minutes>:<seconds> <message>'")
        end
      end
    end
end