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

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

Instance Method Summary collapse

Instance Method Details

#add_preference_hash_values(key, rule_id, user_id) ⇒ Object



186
187
188
189
190
191
192
# File 'tmpsets/set/mod001-core/all/rules.rb', line 186

def add_preference_hash_values key, rule_id, user_id
  @rule_hash[preference_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

#all_user_ids_with_rule_for(set_card, setting_code) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'tmpsets/set/mod001-core/all/rules.rb', line 198

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

#cached_rule_keys_for(user_id) ⇒ Object



194
195
196
# File 'tmpsets/set/mod001-core/all/rules.rb', line 194

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

#clear_preference_cacheObject



273
274
275
276
# File 'tmpsets/set/mod001-core/all/rules.rb', line 273

def clear_preference_cache
  # FIXME: too entwined!
  clear_rule_cache
end

#clear_read_rule_cacheObject



304
305
306
# File 'tmpsets/set/mod001-core/all/rules.rb', line 304

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

#clear_rule_cacheObject



267
268
269
270
271
# File 'tmpsets/set/mod001-core/all/rules.rb', line 267

def clear_rule_cache
  write_rule_cache nil
  write_user_ids_cache nil
  write_rule_keys_cache nil
end

#global_setting(name) ⇒ Object



146
147
148
149
150
# File 'tmpsets/set/mod001-core/all/rules.rb', line 146

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

#interpret_preferencesObject



179
180
181
182
183
184
# File 'tmpsets/set/mod001-core/all/rules.rb', line 179

def interpret_preferences
  ActiveRecord::Base.connection.select_all(preference_sql).each do |row|
    next unless (key = rule_cache_key row) && (user_id = row["user_id"])
    add_preference_hash_values key, row["rule_id"].to_i, user_id.to_i
  end
end

#interpret_simple_rulesObject



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

def interpret_simple_rules
  ActiveRecord::Base.connection.select_all(RULE_SQL).each do |row|
    next unless (key = rule_cache_key row)
    @rule_hash[key] = row["rule_id"].to_i
  end
end

#path_setting(name) ⇒ Object

shouldn't this be in location helper?



152
153
154
155
156
# File 'tmpsets/set/mod001-core/all/rules.rb', line 152

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

#preference_cards(user_name, setting_code) ⇒ Object



226
227
228
# File 'tmpsets/set/mod001-core/all/rules.rb', line 226

def preference_cards user_name, setting_code
  preference_names(user_name, setting_code).map { |name| Card.fetch name }
end

#preference_key(key, user_id) ⇒ Object



245
246
247
# File 'tmpsets/set/mod001-core/all/rules.rb', line 245

def preference_key key, user_id
  "#{key}+#{user_id}"
end

#preference_names(user_name, setting_code) ⇒ Object



215
216
217
218
219
220
221
222
223
224
# File 'tmpsets/set/mod001-core/all/rules.rb', line 215

def preference_names user_name, setting_code
  Card.search(
    { right: { codename: setting_code },
      left: {
        left: { type_id: SetID }, right: user_name
      },
      return: :name
    }, "preference cards for user: #{user_name}"
  )
end

#preference_sql(user_id = nil) ⇒ Object

User-specific rule use the pattern user+set+setting



136
137
138
139
140
141
142
143
144
# File 'tmpsets/set/mod001-core/all/rules.rb', line 136

def preference_sql user_id=nil
  user_restriction =
    if user_id
      "users.id = #{user_id}"
    else
      "users.type_id = #{Card::UserID}"
    end
  PREFERENCE_SQL % user_restriction
end

#read_rule_cacheObject



290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'tmpsets/set/mod001-core/all/rules.rb', line 290

def read_rule_cache
  Card.cache.read("READRULES") || begin
    hash = {}
    Card.connection.select_all(
      Card::Set::All::Rules::READ_RULE_SQL
    ).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

#rule_cacheObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'tmpsets/set/mod001-core/all/rules.rb', line 230

def rule_cache
  Card.cache.read("RULES") || begin
    @rule_hash = {}
    @user_ids_hash = {}
    @rule_keys_hash = {}

    interpret_simple_rules
    interpret_preferences

    write_user_ids_cache @user_ids_hash
    write_rule_keys_cache @rule_keys_hash
    write_rule_cache @rule_hash
  end
end

#rule_cache_key(row) ⇒ Object



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

def rule_cache_key row
  return false unless (setting_code = Codename[row["setting_id"].to_i])

  anchor_id = row["anchor_id"]
  set_class_id = anchor_id.nil? ? row["set_id"] : row["set_tag_id"]
  return false unless (set_class_code = Codename[set_class_id.to_i])

  [anchor_id, set_class_code, setting_code].compact.map(&:to_s) * "+"
end

#rule_keys_cacheObject

all keys of user-specific rules for a given user



259
260
261
262
263
264
265
# File 'tmpsets/set/mod001-core/all/rules.rb', line 259

def rule_keys_cache
  Card.cache.read("RULE_KEYS") || begin
    clear_rule_cache
    rule_cache
    @rule_keys_hash
  end
end

#toggle(val) ⇒ Object



158
159
160
# File 'tmpsets/set/mod001-core/all/rules.rb', line 158

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



250
251
252
253
254
255
256
# File 'tmpsets/set/mod001-core/all/rules.rb', line 250

def user_ids_cache
  Card.cache.read("USER_IDS") || begin
    clear_rule_cache
    rule_cache
    @user_ids_hash
  end
end

#write_rule_cache(hash) ⇒ Object



278
279
280
# File 'tmpsets/set/mod001-core/all/rules.rb', line 278

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

#write_rule_keys_cache(hash) ⇒ Object



286
287
288
# File 'tmpsets/set/mod001-core/all/rules.rb', line 286

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

#write_user_ids_cache(hash) ⇒ Object



282
283
284
# File 'tmpsets/set/mod001-core/all/rules.rb', line 282

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