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



152
153
154
155
156
157
158
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 152

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



168
169
170
171
172
173
174
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 168

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



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

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_idObject



160
161
162
163
164
165
166
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 160

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



142
143
144
145
146
147
148
149
150
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 142

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



176
177
178
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 176

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

#clear_read_rule_cacheObject



303
304
305
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 303

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

#clear_rule_cacheObject



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

def clear_rule_cache
  write_rule_cache nil
  write_user_ids_cache nil
  write_rule_keys_cache nil
end

#clear_user_rule_cacheObject



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

def clear_user_rule_cache
  clear_rule_cache
end

#path_setting(name) ⇒ Object

shouldn’t this be in location helper?



132
133
134
135
136
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 132

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



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

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



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 253

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



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 201

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



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

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

#setting(name) ⇒ Object



126
127
128
129
130
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 126

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



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

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



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

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



197
198
199
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 197

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



223
224
225
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 223

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



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

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



277
278
279
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 277

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

#write_rule_keys_cache(hash) ⇒ Object



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

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

#write_user_ids_cache(hash) ⇒ Object



281
282
283
# File 'tmpsets/set/mod001-01_core/all/rules.rb', line 281

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