Module: Card::Set::All::Rules::ClassMethods

Defined in:
tmpsets/set/mod001-01_core/all/rules.rb

Instance Method Summary collapse

Instance Method Details

#all_rule_keys_with_idObject



155
156
157
158
159
160
161
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 155

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



171
172
173
174
175
176
177
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 171

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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 183

def all_user_ids_with_rule_for set_card, setting_code
  key =
    if (l = set_card.left) && (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_idObject



163
164
165
166
167
168
169
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 163

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



145
146
147
148
149
150
151
152
153
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 145

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



179
180
181
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 179

def cached_rule_keys_for user_id
  rule_keys_cache[user_id] || []
end

#clear_read_rule_cacheObject



310
311
312
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 310

def clear_read_rule_cache
  Card.cache.write 'READRULES', nil
end

#clear_rule_cacheObject



250
251
252
253
254
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 250

def clear_rule_cache
  write_rule_cache nil
  write_user_ids_cache nil
  write_rule_keys_cache nil
end

#clear_user_rule_cacheObject



256
257
258
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 256

def clear_user_rule_cache
  clear_rule_cache
end

#global_setting(name) ⇒ Object



129
130
131
132
133
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 129

def global_setting name
  Auth.as_bot do
    (card = Card[name]) && !card.db_content.strip.empty? && card.db_content
  end
end

#path_setting(name) ⇒ Object

shouldn’t this be in location helper?



135
136
137
138
139
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 135

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_cacheObject



296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 296

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 = row['party_id'].to_i
      hash[party_id] ||= []
      hash[party_id] << row['read_rule_id'].to_i
    end
    Card.cache.write 'READRULES', hash
  end
end

#refresh_rule_cache_for_user(user_id) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 260

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_cacheObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 208

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_cacheObject

all keys of user-specific rules for a given user



243
244
245
246
247
248
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 243

def rule_keys_cache
  Card.cache.read('RULE_KEYS') || begin
    rule_cache
    Card.cache.read('RULE_KEYS')
  end
end

#toggle(val) ⇒ Object



141
142
143
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 141

def toggle val
  val.to_s.strip == '1'
end

#user_ids_cacheObject

all users that have a user-specific rule for a given rule key



235
236
237
238
239
240
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 235

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



200
201
202
203
204
205
206
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 200

def user_rule_cards user_name, setting_code
  Card.search(
    { right: { codename: setting_code },
      left: { left: { type_id: SetID }, right: user_name }
      }, "rule cards for user: #{user_name}"
  )
end

#user_rule_key(key, user_id) ⇒ Object



230
231
232
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 230

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



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
123
124
125
126
127
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 98

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 settings.type_id = #{Card::SettingID}
      and (#{user_restriction} or users.codename = 'all')
      and sets.trash       is false
      and settings.trash   is false
      and users.trash      is false
      and user_sets.trash  is false
      and user_rules.trash is false;
  }
end

#write_rule_cache(hash) ⇒ Object



284
285
286
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 284

def write_rule_cache hash
  Card.cache.write 'RULES', hash
end

#write_rule_keys_cache(hash) ⇒ Object



292
293
294
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 292

def write_rule_keys_cache hash
  Card.cache.write 'RULE_KEYS', hash
end

#write_user_ids_cache(hash) ⇒ Object



288
289
290
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 288

def write_user_ids_cache hash
  Card.cache.write 'USER_IDS', hash
end