Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 390
- Log:
Initial import of Canvass, a donations-based poll application.
- Author:
- rool
- Date:
- Mon Mar 21 14:58:04 +0000 2011
- Size:
- 5340 Bytes
1 | ######################################################################## |
2 | # File:: user.rb |
3 | # (C):: Hipposoft 2011 |
4 | # |
5 | # Purpose:: Descriptions of users, cached from Hub information. |
6 | # ---------------------------------------------------------------------- |
7 | # 30-Jan-2011 (ADH): Created. |
8 | ######################################################################## |
9 | |
10 | class User < ActiveRecord::Base |
11 | |
12 | # =========================================================================== |
13 | # CHARACTERISTICS |
14 | # =========================================================================== |
15 | |
16 | serialize :preferences |
17 | |
18 | has_many :polls |
19 | has_many :donations |
20 | |
21 | MAXLEN_NAME = 150 |
22 | MAXLEN_EMAIL = 200 |
23 | |
24 | validates_presence_of :name |
25 | validates_length_of :name, :within => 1..MAXLEN_NAME |
26 | |
27 | validates_presence_of :email |
28 | validates_uniqueness_of :email |
29 | validates_length_of :email, :within => 6..MAXLEN_EMAIL # 6 => "r@a.wk" |
30 | |
31 | # Almost nothing is accessible to mass assignment. |
32 | |
33 | attr_accessible( |
34 | :name |
35 | ) |
36 | |
37 | # See Jason King's "good_sort" plugin: |
38 | # |
39 | # http://github.com/JasonKing/good_sort/tree/master |
40 | # |
41 | # Must use "table_exists?", as good_sort needs to check the database but |
42 | # this class may be examined by migrations before the table is created. |
43 | |
44 | sort_on :name, |
45 | :email, |
46 | :admin if User.table_exists? |
47 | |
48 | # How many entries to list per index page? See the Will Paginate plugin: |
49 | # |
50 | # http://wiki.github.com/mislav/will_paginate |
51 | # |
52 | def self.per_page |
53 | MAXIMUM_LIST_ITEMS_PER_PAGE |
54 | end |
55 | |
56 | # Search columns for views rendering the "shared/_simple_search.html.erb" |
57 | # view partial and using "appctrl_build_search_conditions" to handle queries. |
58 | |
59 | SEARCH_COLUMNS = %w{name email} |
60 | |
61 | # =========================================================================== |
62 | # PERMISSIONS |
63 | # =========================================================================== |
64 | |
65 | # Write-only public accessor methods. |
66 | |
67 | def email=( value ) |
68 | write_attribute :email, ( value ? value.downcase : nil ) |
69 | end |
70 | |
71 | # =========================================================================== |
72 | # GENERAL |
73 | # =========================================================================== |
74 | |
75 | # Apply a default sort to the given array of model instances. The array is |
76 | # modified in place. |
77 | # |
78 | def self.apply_default_sort_order( array ) |
79 | array.sort! { | x, y | x.name <=> y.name } |
80 | end |
81 | |
82 | # Get a value from the user's preferences hash. The hash is nested in a |
83 | # similar manner to the I18n module's translation hashes and is addressed |
84 | # in a similar way - pass a dot-separated key string, e.g. "foo.bar.baz". |
85 | # Returns 'nil' for unset preferences, else the value at that location. |
86 | # |
87 | # Currently defined preferences include (but may not be limited to - this |
88 | # list may be out of date) the following: |
89 | # |
90 | # sorting => { <controller-name> => { <good_sort params hash entry> } } |
91 | # |
92 | # Most recently recorded value of params[:sort] by an index action for |
93 | # a controller identified by <controller_name>. For more information see |
94 | # "appctrl_apply_sorting_preferences". |
95 | # |
96 | # bounceback => nil or hash of bounceback data |
97 | # |
98 | # Enforced visit of a particular URL is in progress unless nil. For more |
99 | # information see "appctrl_enable_bounceback". |
100 | # |
101 | # masspay => nil or hash of mass payment data |
102 | # |
103 | # A mass payment session is in progress unless nil. For more information |
104 | # see the Payment Bulk Onsite Controller. |
105 | # |
106 | def get_preference( key_str ) |
107 | keys = key_str.split( '.' ) |
108 | pref = self.preferences |
109 | |
110 | for key in keys |
111 | return nil if pref.nil? |
112 | pref = pref[ key ] |
113 | end |
114 | |
115 | return pref |
116 | end |
117 | |
118 | # Set the value of a preference identified as for "get_preference" above. |
119 | # If any of the nested hashes identified by the key string are missing (e.g. |
120 | # in example "foo.bar.baz", any of hashes "foo", "bar" or "baz") then |
121 | # relevant entries in the preferences will be made automatically. |
122 | # |
123 | # The method saves 'self' back to database and returns the return value of |
124 | # the call made to "save". Thus returns 'false' on failure, else 'true'. |
125 | # |
126 | # See also "set_preference!". |
127 | # |
128 | def set_preference( key_str, value ) |
129 | return set_preference_by_method!( key_str, value, :save ) |
130 | end |
131 | |
132 | # As "set_preference" but returns the result of a call to "save!", so raises |
133 | # an exception on failure. |
134 | # |
135 | def set_preference!( key_str, value ) |
136 | return set_preference_by_method!( key_str, value, :save! ) |
137 | end |
138 | |
139 | # =========================================================================== |
140 | # PRIVATE |
141 | # =========================================================================== |
142 | |
143 | private |
144 | |
145 | # Implement "set_preference" and "set_preference!" - pass the preference |
146 | # key string, preference value and ":send" or ":send!" depending on the |
147 | # method required for saving the preference changes. |
148 | # |
149 | def set_preference_by_method!( key_str, value, method ) |
150 | keys = key_str.split( '.' ) |
151 | root = self.preferences || {} |
152 | pref = root |
153 | |
154 | keys.each_index do | index | |
155 | key = keys[ index ] |
156 | |
157 | if ( index == keys.size - 1 ) |
158 | pref[ key ] = value |
159 | else |
160 | pref[ key ] ||= {} |
161 | pref = pref[ key ] |
162 | end |
163 | end |
164 | |
165 | self.preferences = root |
166 | return self.send( method ) |
167 | end |
168 | end |