Class: Elt

Inherits:
ActiveRecord::Base
  • Object
show all
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

An element is linked to:

  • mail

  • person

  • voters

  • choices

  • attachments

  • subscribers

The element id is generated from the subject for better readability (but only normal characters are used)

Instance Method Summary collapse

Instance Method Details

#add_child(c) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/elt.rb', line 104

def add_child(c)
	result = super(c)
	p = c
	while p
		if !p.last_activity or p.last_activity < c.created_on then
			p.last_activity = c.created_on
			p.save!
		end
		p = p.parent
	end
	result
end

#all_recipientsObject

Just a quick method to get all subscribers as a simple list



38
39
40
41
42
43
44
# File 'app/models/elt.rb', line 38

def all_recipients
	if parent
		(subscribers + parent.all_recipients).uniq
	else
		subscribers
	end
end

#all_subscriptionsObject



46
47
48
49
50
51
52
# File 'app/models/elt.rb', line 46

def all_subscriptions
	if parent
		(subscriptions + parent.all_subscriptions).uniq
	else
		subscriptions
	end
end

#before_createObject



92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/elt.rb', line 92

def before_create
	# Let's generate a nice looking id
	self.id ||= new_id =
		subject.gsub(/\[[\w-]*\]/, '').strip \
		.gsub(/\s/, '_').gsub(/[^\w]+/, '') \
		.gsub(/_+/, '_').gsub(/(^_|_$)/, '') if subject
	self.id ||= "elt"

	i = 0
	self.id = "#{new_id}_#{i+=1}" while self.class.find_by_id self.id
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
|   |___________________________|   |___________________________|   |
|___________________________________________________________________|


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/models/elt.rb', line 128

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

#publishObject

Mail this elt to all subscribers



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/elt.rb', line 71

def publish
	logger.info "Publish #{subject}"

	self.position = Regexp.last_match(1) if body =~ /^\s*position\s*:\s*(\d+(\.\d+)?)(\s|$)/

	build_mail(:elt => self) unless mail
	mail.publish
	save!

	parent.vote Regexp.last_match(1), person if body =~ /^\s*(-1|0|\+1)(\s|$)/
	if Regexp.last_match(1) and self.body.gsub(/(-1|0|\+1)/, '').strip.empty?
		# Hide simple votes
		vote -1
	else
		vote if person
	end

	parent.add_child(self) unless lft or rgt
	true
end

#result(electoralList = nil) ⇒ Object

Get the vote results



63
64
65
66
67
68
# File 'app/models/elt.rb', line 63

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}'") || 0
end

#vote(value = 1, person = self.person) ⇒ Object



54
55
56
57
58
59
60
# File 'app/models/elt.rb', line 54

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!
	mail.publish if mail
end