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
150 151 152 153 154 155 156 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 150 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
166 167 168 169 170 171 172 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 166 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
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 178 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
158 159 160 161 162 163 164 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 158 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
140 141 142 143 144 145 146 147 148 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 140 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
174 175 176 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 174 def cached_rule_keys_for user_id rule_keys_cache[user_id] || [] end |
#clear_read_rule_cache ⇒ Object
301 302 303 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 301 def clear_read_rule_cache Card.cache.write 'READRULES', nil end |
#clear_rule_cache ⇒ Object
241 242 243 244 245 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 241 def clear_rule_cache write_rule_cache nil write_user_ids_cache nil write_rule_keys_cache nil end |
#clear_user_rule_cache ⇒ Object
247 248 249 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 247 def clear_user_rule_cache clear_rule_cache end |
#path_setting(name) ⇒ Object
shouldn’t this be in location helper?
130 131 132 133 134 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 130 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
289 290 291 292 293 294 295 296 297 298 299 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 289 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
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 251 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
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 199 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
234 235 236 237 238 239 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 234 def rule_keys_cache Card.cache.read('RULE_KEYS') || begin rule_cache Card.cache.read('RULE_KEYS') end end |
#setting(name) ⇒ Object
124 125 126 127 128 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 124 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
136 137 138 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 136 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
226 227 228 229 230 231 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 226 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
195 196 197 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 195 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
221 222 223 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 221 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
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 121 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 95 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
275 276 277 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 275 def write_rule_cache hash Card.cache.write 'RULES', hash end |
#write_rule_keys_cache(hash) ⇒ Object
283 284 285 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 283 def write_rule_keys_cache hash Card.cache.write 'RULE_KEYS', hash end |
#write_user_ids_cache(hash) ⇒ Object
279 280 281 |
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 279 def write_user_ids_cache hash Card.cache.write 'USER_IDS', hash end |