Class: Elt
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Elt
- Defined in:
- app/models/elt.rb
Overview
This is, in fact, the main parlement class!
It holds all posts, that’s mostly a subject and a body, plus their relationships in a tree of elements
Instance Method Summary collapse
-
#all_recipients ⇒ Object
Just a quick method to get all subscribers as a simple list.
-
#move(destination) ⇒ Object
_ | Root | | __ __ | | | Child 1 | | Child 2 | | | | __ _ | | __ _ | | | | | C 1.1 | | C 1.2 | | | | C 2.1 | | C 2.2 | | | 1 2 3_________4 5________6 7 8 9_________10 11_______12 13 14 | |_| |_| | |_|.
-
#publish ⇒ Object
Mail this elt to all subscribers.
-
#result(electoralList = nil) ⇒ Object
Get the vote results.
- #save ⇒ Object
- #vote(value = 1, person = self.person) ⇒ Object
Instance Method Details
#all_recipients ⇒ Object
Just a quick method to get all subscribers as a simple list
24 25 26 27 28 29 30 |
# File 'app/models/elt.rb', line 24 def all_recipients if parent (subscribers + parent.all_recipients).uniq else subscribers end end |
#move(destination) ⇒ Object
_
| Root |
| ____________________________ ____________________________ |
| | Child 1 | | Child 2 | |
| | __________ _________ | | __________ _________ | |
| | | C 1.1 | | C 1.2 | | | | C 2.1 | | C 2.2 | | |
1 2 3_________4 5________6 7 8 9_________10 11_______12 13 14
| |___________________________| |___________________________| |
|___________________________________________________________________|
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 114 115 116 117 118 119 120 |
# File 'app/models/elt.rb', line 88 def move(destination) return if self[right_col_name].nil? || self[left_col_name].nil? \ || destination[right_col_name].nil? || destination[left_col_name].nil? \ || (self[left_col_name] < destination[left_col_name] && destination[right_col_name] < self[right_col_name]) dif = self[right_col_name] - self[left_col_name] + 1 moveBy = destination[right_col_name] - self[left_col_name] self.class.transaction { # Take out of range this very node self.class.update_all( "#{left_col_name} = -#{left_col_name}, #{right_col_name} = -#{right_col_name}", "#{scope_condition} AND #{self[left_col_name]} <= #{left_col_name} AND #{right_col_name} <= #{self[right_col_name]}" ) # Shift intermediary elements to the left self.class.update_all( "#{left_col_name} = (#{left_col_name} - #{dif})", "#{scope_condition} AND #{self[right_col_name]} < #{left_col_name} AND #{left_col_name} < #{destination[right_col_name]}" ) self.class.update_all( "#{right_col_name} = (#{right_col_name} - #{dif})", "#{scope_condition} AND #{self[right_col_name]} < #{right_col_name} AND #{right_col_name} < #{destination[right_col_name]}" ) # Shift intermediary elements to the right self.class.update_all( "#{left_col_name} = (#{left_col_name} + #{dif})", "#{scope_condition} AND #{destination[right_col_name]} < #{left_col_name} AND #{left_col_name} < #{self[left_col_name]}" ) self.class.update_all( "#{right_col_name} = (#{right_col_name} + #{dif})", "#{scope_condition} AND #{destination[right_col_name]} <= #{right_col_name} AND #{right_col_name} < #{self[left_col_name]}" ) # Puts back this element where it should be self.class.update_all( "#{left_col_name} = -#{left_col_name} + #{moveBy}, #{right_col_name} = -#{right_col_name} + #{moveBy}", "#{scope_condition} AND #{self[left_col_name]} <= -#{left_col_name} AND #{right_col_name} <= -#{self[right_col_name]}" ) self.parent = destination self.save! } end |
#publish ⇒ Object
Mail this elt to all subscribers
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/models/elt.rb', line 48 def publish logger.info "Publish #{subject}" build_mail(:elt => self) unless mail mail.publish save! parent.vote Regexp.last_match(1), person if body =~ /^\s*(-1|0|\+1)(\s|$)/ if self.body.gsub(/(-1|0|\+1)/, '').strip.empty? # Hide simple votes vote -1 else vote if person end true end |
#result(electoralList = nil) ⇒ Object
Get the vote results
40 41 42 43 44 45 |
# File 'app/models/elt.rb', line 40 def result(electoralList = nil) #(Choice.count("elt_id = '#{self.id}' AND value = 1") \ # - \ # Choice.count("elt_id = '#{self.id}' AND value = -1")) Choice.sum(:value, :conditions => "elt_id = '#{self.id}'") end |
#save ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/models/elt.rb', line 65 def save if new_record? and subject and not subject.empty? # Let's generate a nice looking id self.id = new_id = subject.gsub(/\[[\w-]*\]/, '').strip.gsub(/[\s\?\&\#\\\/:]+/, '_') discrim = 0 self.id = "#{new_id}_#{discrim}" while discrim += 1 and self.class.find_by_id self.id end super end |
#vote(value = 1, person = self.person) ⇒ Object
32 33 34 35 36 37 |
# File 'app/models/elt.rb', line 32 def vote(value = 1, person = self.person) logger.info "#{person.name if person} vote #{value} on #{subject} #{self.choices}" choices.each{ |c| c.destroy if c.person == person } choice = choices.build :value => value, :person => person choice.save! end |