Module: Card::Set::All::Rules::ClassMethods
- Defined in:
- tmpsets/set/mod001-01_core/all/rules.rb
Instance Method Summary collapse
- #all_rule_keys_with_id ⇒ Object
- #all_rule_keys_with_id_for(user_id) ⇒ Object
- #all_user_ids_with_rule_for(set_card, setting_code) ⇒ Object
- #all_user_rule_keys_with_id_and_user_id ⇒ Object
- #cache_key(row) ⇒ Object
- #cached_rule_keys_for(user_id) ⇒ Object
- #clear_read_rule_cache ⇒ Object
- #clear_rule_cache ⇒ Object
- #clear_user_rule_cache ⇒ Object
-
#path_setting(name) ⇒ Object
shouldn’t this be in location helper?.
- #read_rule_cache ⇒ Object
- #refresh_rule_cache_for_user(user_id) ⇒ Object
- #rule_cache ⇒ Object
-
#rule_keys_cache ⇒ Object
all keys of user-specific rules for a given user.
- #setting(name) ⇒ Object
- #toggle(val) ⇒ Object
-
#user_ids_cache ⇒ Object
all users that have a user-specific rule for a given rule key.
- #user_rule_cards(user_name, setting_code) ⇒ Object
- #user_rule_key(key, user_id) ⇒ Object
-
#user_rule_sql(user_id = nil) ⇒ Object
User-specific rule use the pattern user+set+setting.
- #write_rule_cache(hash) ⇒ Object
- #write_rule_keys_cache(hash) ⇒ Object
- #write_user_ids_cache(hash) ⇒ Object
Instance Method Details
#all_rule_keys_with_id ⇒ Object
151 152 153 154 155 156 157 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 151 def all_rule_keys_with_id ActiveRecord::Base.connection.select_all(RuleSQL).each do |row| if key = cache_key(row) yield(key, row['rule_id'].to_i) end end end |
#all_rule_keys_with_id_for(user_id) ⇒ Object
167 168 169 170 171 172 173 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 167 def all_rule_keys_with_id_for user_id ActiveRecord::Base.connection.select_all(user_rule_sql(user_id)).each do |row| if key = cache_key(row) yield(key, row['rule_id'].to_i) end end end |
#all_user_ids_with_rule_for(set_card, setting_code) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 179 def all_user_ids_with_rule_for set_card, setting_code key = if (l=set_card.left) and (r=set_card.right) set_class_code = Codename[ r.id ] "#{l.id}+#{set_class_code}+#{setting_code}" else set_class_code = Codename[ set_card.id ] "#{set_class_code}+#{setting_code}" end user_ids = user_ids_cache[key] || [] if user_ids.include? AllID # rule for all -> return all user ids Card.where(type_id: UserID).pluck(:id) else user_ids end end |
#all_user_rule_keys_with_id_and_user_id ⇒ Object
159 160 161 162 163 164 165 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 159 def all_user_rule_keys_with_id_and_user_id ActiveRecord::Base.connection.select_all(user_rule_sql).each do |row| if key = cache_key(row) and user_id = row['user_id'] yield(key, row['rule_id'].to_i, user_id.to_i) end end end |
#cache_key(row) ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 141 def cache_key row setting_code = Codename[ row['setting_id'].to_i ] or return false anchor_id = row['anchor_id'] set_class_id = anchor_id.nil? ? row['set_id'] : row['set_tag_id'] set_class_code = Codename[ set_class_id.to_i ] or return false key_base = [ anchor_id, set_class_code, setting_code].compact.map( &:to_s ) * '+' end |
#cached_rule_keys_for(user_id) ⇒ Object
175 176 177 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 175 def cached_rule_keys_for user_id rule_keys_cache[user_id] || [] end |
#clear_read_rule_cache ⇒ Object
302 303 304 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 302 def clear_read_rule_cache Card.cache.write 'READRULES', nil end |
#clear_rule_cache ⇒ Object
242 243 244 245 246 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 242 def clear_rule_cache write_rule_cache nil write_user_ids_cache nil write_rule_keys_cache nil end |
#clear_user_rule_cache ⇒ Object
248 249 250 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 248 def clear_user_rule_cache clear_rule_cache end |
#path_setting(name) ⇒ Object
shouldn’t this be in location helper?
131 132 133 134 135 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 131 def path_setting name #shouldn't this be in location helper? name ||= '/' return name if name =~ /^(http|mailto)/ "#{Card.config.relative_url_root}#{name}" end |
#read_rule_cache ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 290 def read_rule_cache Card.cache.read('READRULES') || begin hash = {} ActiveRecord::Base.connection.select_all( Card::Set::All::Rules::ReadRuleSQL ).each do |row| party_id, read_rule_id = row['party_id'].to_i, row['read_rule_id'].to_i hash[party_id] ||= [] hash[party_id] << read_rule_id end Card.cache.write 'READRULES', hash end end |
#refresh_rule_cache_for_user(user_id) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 252 def refresh_rule_cache_for_user user_id rule_hash = rule_cache user_ids_hash = user_ids_cache rule_keys_hash = rule_keys_cache cached_rule_keys_for(user_id).each do |key| rule_hash[ user_rule_key(key, user_id) ] = nil user_ids_hash[ key ].delete(user_id) end rule_keys_hash[ user_id ] = nil all_rule_keys_with_id_for(user_id) do |key, rule_id| rule_hash[ user_rule_key(key,user_id) ] = rule_id user_ids_hash[ key ] ||= [] user_ids_hash[ key ] << user_id rule_keys_hash[ user_id ] ||= [] rule_keys_hash[ user_id ] << key end write_user_ids_cache user_ids_hash write_rule_keys_cache rule_keys_hash write_rule_cache rule_hash end |
#rule_cache ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 200 def rule_cache Card.cache.read('RULES') || begin rule_hash = {} all_rule_keys_with_id do |key,rule_id| rule_hash[key] = rule_id end user_ids_hash = {} rule_keys_hash = {} all_user_rule_keys_with_id_and_user_id do |key, rule_id, user_id| rule_hash[ user_rule_key(key,user_id) ] = rule_id user_ids_hash[key] ||= [] user_ids_hash[key] << user_id rule_keys_hash[user_id] ||= [] rule_keys_hash[user_id] << key end write_user_ids_cache user_ids_hash write_rule_keys_cache rule_keys_hash write_rule_cache rule_hash end end |
#rule_keys_cache ⇒ Object
all keys of user-specific rules for a given user
235 236 237 238 239 240 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 235 def rule_keys_cache Card.cache.read('RULE_KEYS') || begin rule_cache Card.cache.read('RULE_KEYS') end end |
#setting(name) ⇒ Object
125 126 127 128 129 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 125 def setting name Auth.as_bot do card=Card[name] and !card.db_content.strip.empty? and card.db_content end end |
#toggle(val) ⇒ Object
137 138 139 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 137 def toggle val val.to_s.strip == '1' end |
#user_ids_cache ⇒ Object
all users that have a user-specific rule for a given rule key
227 228 229 230 231 232 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 227 def user_ids_cache Card.cache.read('USER_IDS') || begin rule_cache Card.cache.read('USER_IDS') end end |
#user_rule_cards(user_name, setting_code) ⇒ Object
196 197 198 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 196 def user_rule_cards user_name, setting_code Card.search right: {codename: setting_code}, left: {left: {type_id: SetID}, right: user_name} end |
#user_rule_key(key, user_id) ⇒ Object
222 223 224 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 222 def user_rule_key key, user_id "#{key}+#{user_id}" end |
#user_rule_sql(user_id = nil) ⇒ Object
User-specific rule use the pattern user+set+setting
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 121 122 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 96 def user_rule_sql user_id=nil user_restriction = if user_id "users.id = #{user_id}" else "users.type_id = #{Card::UserID}" end %{ select user_rules.id as rule_id, settings.id as setting_id, sets.id as set_id, sets.left_id as anchor_id, sets.right_id as set_tag_id, users.id as user_id from cards user_rules join cards user_sets on user_rules.left_id = user_sets.id join cards settings on user_rules.right_id = settings.id join cards users on user_sets.right_id = users.id join cards sets on user_sets.left_id = sets.id where sets.type_id = #{Card::SetID } and sets.trash is false and settings.type_id = #{Card::SettingID} and settings.trash is false and ( #{user_restriction} or users.codename = 'all' ) and users.trash is false and user_sets.trash is false and user_rules.trash is false; } end |
#write_rule_cache(hash) ⇒ Object
276 277 278 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 276 def write_rule_cache hash Card.cache.write 'RULES', hash end |
#write_rule_keys_cache(hash) ⇒ Object
284 285 286 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 284 def write_rule_keys_cache hash Card.cache.write 'RULE_KEYS', hash end |
#write_user_ids_cache(hash) ⇒ Object
280 281 282 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 280 def write_user_ids_cache hash Card.cache.write 'USER_IDS', hash end |