card 1.17.3 → 1.17.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0761ae5ad975f7da3a5a1fb311af162763f2a499
4
- data.tar.gz: 9c1da842848e3cb3e4bc8bd970cc4833672ef1ec
3
+ metadata.gz: 71d8c48249e77df2b401a40899bb6174cbafa0bc
4
+ data.tar.gz: 2214e2225cad7712ff1d7f93b32b812f392d15d0
5
5
  SHA512:
6
- metadata.gz: fc4501bfb58f3f77921f2efba3361c36bb6b1353f891c9949e2a2ef518535c714c07b9310c43db054d1314bb4104c0b6b3cbb8131b33ce43f05d7b0f6dcaafe5
7
- data.tar.gz: e36ada3b20b49e0fc72b7e1478daed3dd7b2fc5e37ffb1eb610ebb8f12932e31e44d03bca318b3994ec5fda1dbec76ddbde8ad7a49b4171adcc9b1d36011ada8
6
+ metadata.gz: e77682bdebca32646c7dc1866d56e57b1aaca3496fcceaf028ba842313a1c667d0d178e6cda84213fbce9982abe12c016d1f78ecaf01954f75a4801114bfd052
7
+ data.tar.gz: 0d1db3905c69508ba95f54f75f5c61f3ee660f3ccca2f19cb48f22d6f14f5c71730e3bde023ad909fc951e357b3f12f19f5518caeaca36b63caf52d3d6b252fa
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.17.3
1
+ 1.17.4
@@ -1,45 +1,82 @@
1
- RuleSQL = %{
2
- select rules.id as rule_id, settings.id as setting_id, sets.id as set_id,
3
- sets.left_id as anchor_id, sets.right_id as set_tag_id
4
- from cards rules
5
- join cards sets on rules.left_id = sets.id
6
- join cards settings on rules.right_id = settings.id
7
- where sets.type_id = #{Card::SetID } and sets.trash is false
8
- and settings.type_id = #{Card::SettingID} and settings.trash is false
9
- and rules.trash is false
10
- and (settings.codename != 'follow' or rules.db_content != '');
11
- }
12
-
13
- ReadRuleSQL = %{
14
- select refs.referee_id as party_id, read_rules.id as read_rule_id
15
- from cards read_rules
16
- join card_references refs on refs.referer_id = read_rules.id
17
- join cards sets on read_rules.left_id = sets.id
18
- where read_rules.right_id = #{Card::ReadID} and read_rules.trash is false and sets.type_id = #{Card::SetID};
19
- }
1
+ # frozen_string_literal: true
2
+
3
+ RULE_SQL = %(
4
+ SELECT
5
+ rules.id AS rule_id,
6
+ settings.id AS setting_id,
7
+ sets.id AS set_id,
8
+ sets.left_id AS anchor_id,
9
+ sets.right_id AS set_tag_id
10
+ FROM cards rules
11
+ JOIN cards sets ON rules.left_id = sets.id
12
+ JOIN cards settings ON rules.right_id = settings.id
13
+ WHERE sets.type_id = #{Card::SetID}
14
+ AND settings.type_id = #{Card::SettingID}
15
+ AND (settings.codename != 'follow' OR rules.db_content != '')
16
+ AND rules.trash is false
17
+ AND sets.trash is false
18
+ AND settings.trash is false;
19
+ ).freeze
20
+
21
+ # FIXME: "follow" hardcoded above
22
+
23
+ READ_RULE_SQL = %(
24
+ SELECT
25
+ refs.referee_id AS party_id,
26
+ read_rules.id AS read_rule_id
27
+ FROM cards read_rules
28
+ JOIN card_references refs ON refs.referer_id = read_rules.id
29
+ JOIN cards sets ON read_rules.left_id = sets.id
30
+ WHERE read_rules.right_id = #{Card::ReadID}
31
+ AND sets.type_id = #{Card::SetID}
32
+ AND read_rules.trash is false
33
+ AND sets.trash is false;
34
+ ).freeze
35
+
36
+ PREFERENCE_SQL = %(
37
+ SELECT
38
+ preferences.id AS rule_id,
39
+ settings.id AS setting_id,
40
+ sets.id AS set_id,
41
+ sets.left_id AS anchor_id,
42
+ sets.right_id AS set_tag_id,
43
+ users.id AS user_id
44
+ FROM cards preferences
45
+ JOIN cards user_sets ON preferences.left_id = user_sets.id
46
+ JOIN cards settings ON preferences.right_id = settings.id
47
+ JOIN cards users ON user_sets.right_id = users.id
48
+ JOIN cards sets ON user_sets.left_id = sets.id
49
+ WHERE sets.type_id = #{Card::SetID}
50
+ AND settings.type_id = #{Card::SettingID}
51
+ AND (%s or users.codename = 'all')
52
+ AND sets.trash is false
53
+ AND settings.trash is false
54
+ AND users.trash is false
55
+ AND user_sets.trash is false
56
+ AND preferences.trash is false;
57
+ ).freeze
20
58
 
21
59
  def is_rule?
22
- is_standard_rule? || is_user_rule?
60
+ is_standard_rule? || is_preference?
23
61
  end
24
62
 
25
63
  def is_standard_rule?
26
- (r = right( skip_modules: true )) &&
27
- r.type_id == Card::SettingID &&
28
- (l = left( skip_modules: true )) &&
64
+ (r = right(skip_modules: true)) &&
65
+ r.type_id == Card::SettingID &&
66
+ (l = left(skip_modules: true)) &&
29
67
  l.type_id == Card::SetID
30
68
  end
31
69
 
32
- def is_user_rule?
33
- cardname.parts.length > 2 &&
34
- (r = right( skip_modules: true )) &&
35
- r.type_id == Card::SettingID &&
36
- (set = self[0..-3, skip_modules: true]) &&
37
- set.type_id == Card::SetID &&
38
- (user = self[-2, skip_modules: true] ) &&
39
- (user.type_id == Card::UserID || user.codename == 'all' )
70
+ def is_preference?
71
+ cardname.parts.length > 2 &&
72
+ (r = right(skip_modules: true)) &&
73
+ r.type_id == Card::SettingID &&
74
+ (set = self[0..-3, skip_modules: true]) &&
75
+ set.type_id == Card::SetID &&
76
+ (user = self[-2, skip_modules: true]) &&
77
+ (user.type_id == Card::UserID || user.codename == 'all')
40
78
  end
41
79
 
42
-
43
80
  def rule setting_code, options={}
44
81
  options[:skip_modules] = true
45
82
  card = rule_card setting_code, options
@@ -51,14 +88,10 @@ def rule_card setting_code, options={}
51
88
  end
52
89
 
53
90
  def rule_card_id setting_code, options={}
54
- fallback = options.delete( :fallback )
91
+ fallback = options.delete :fallback
55
92
 
56
93
  if Card::Setting.user_specific? setting_code
57
- user_id = options[:user_id] || (options[:user] and options[:user].id) || Auth.current_id
58
- if user_id
59
- fallback = "#{setting_code}+#{AllID}"
60
- setting_code = "#{setting_code}+#{user_id}"
61
- end
94
+ fallback, setting_code = preference_card_id_lookups setting_code, options
62
95
  end
63
96
 
64
97
  rule_set_keys.each do |rule_set_key|
@@ -69,7 +102,15 @@ def rule_card_id setting_code, options={}
69
102
  nil
70
103
  end
71
104
 
72
- def related_sets with_self = false
105
+ def preference_card_id_lookups setting_code, options={}
106
+ user_id = options[:user_id] ||
107
+ (options[:user] && options[:user].id) ||
108
+ Auth.current_id
109
+ return unless user_id
110
+ ["#{setting_code}+#{AllID}", "#{setting_code}+#{user_id}"]
111
+ end
112
+
113
+ def related_sets with_self=false
73
114
  # refers to sets that users may configure from the current card -
74
115
  # NOT to sets to which the current card belongs
75
116
 
@@ -89,38 +130,16 @@ def related_sets with_self = false
89
130
  end
90
131
 
91
132
  module ClassMethods
92
-
93
133
  # User-specific rule use the pattern
94
134
  # user+set+setting
95
- def user_rule_sql user_id=nil
96
- user_restriction = if user_id
135
+ def preference_sql user_id=nil
136
+ user_restriction =
137
+ if user_id
97
138
  "users.id = #{user_id}"
98
139
  else
99
140
  "users.type_id = #{Card::UserID}"
100
141
  end
101
-
102
- %{
103
- select
104
- user_rules.id as rule_id,
105
- settings.id as setting_id,
106
- sets.id as set_id,
107
- sets.left_id as anchor_id,
108
- sets.right_id as set_tag_id,
109
- users.id as user_id
110
- from cards user_rules
111
- join cards user_sets on user_rules.left_id = user_sets.id
112
- join cards settings on user_rules.right_id = settings.id
113
- join cards users on user_sets.right_id = users.id
114
- join cards sets on user_sets.left_id = sets.id
115
- where sets.type_id = #{Card::SetID }
116
- and settings.type_id = #{Card::SettingID}
117
- and (#{user_restriction} or users.codename = 'all')
118
- and sets.trash is false
119
- and settings.trash is false
120
- and users.trash is false
121
- and user_sets.trash is false
122
- and user_rules.trash is false;
123
- }
142
+ PREFERENCE_SQL % user_restriction
124
143
  end
125
144
 
126
145
  def global_setting name
@@ -129,7 +148,7 @@ module ClassMethods
129
148
  end
130
149
  end
131
150
 
132
- def path_setting name #shouldn't this be in location helper?
151
+ def path_setting name # shouldn't this be in location helper?
133
152
  name ||= '/'
134
153
  return name if name =~ /^(http|mailto)/
135
154
  "#{Card.config.relative_url_root}#{name}"
@@ -139,38 +158,36 @@ module ClassMethods
139
158
  val.to_s.strip == '1'
140
159
  end
141
160
 
142
- def cache_key row
143
- setting_code = Codename[ row['setting_id'].to_i ] or return false
161
+ def rule_cache_key row
162
+ return false unless (setting_code = Codename[row['setting_id'].to_i])
144
163
 
145
164
  anchor_id = row['anchor_id']
146
165
  set_class_id = anchor_id.nil? ? row['set_id'] : row['set_tag_id']
147
- set_class_code = Codename[ set_class_id.to_i ] or return false
166
+ return false unless (set_class_code = Codename[set_class_id.to_i])
148
167
 
149
- key_base = [ anchor_id, set_class_code, setting_code].compact.map( &:to_s ) * '+'
168
+ [anchor_id, set_class_code, setting_code].compact.map(&:to_s) * '+'
150
169
  end
151
170
 
152
- def all_rule_keys_with_id
153
- ActiveRecord::Base.connection.select_all(RuleSQL).each do |row|
154
- if key = cache_key(row)
155
- yield(key, row['rule_id'].to_i)
156
- end
171
+ def interpret_simple_rules
172
+ ActiveRecord::Base.connection.select_all(RULE_SQL).each do |row|
173
+ next unless (key = rule_cache_key row)
174
+ @rule_hash[key] = row['rule_id'].to_i
157
175
  end
158
176
  end
159
177
 
160
- def all_user_rule_keys_with_id_and_user_id
161
- ActiveRecord::Base.connection.select_all(user_rule_sql).each do |row|
162
- if key = cache_key(row) and user_id = row['user_id']
163
- yield(key, row['rule_id'].to_i, user_id.to_i)
164
- end
178
+ def interpret_preferences
179
+ ActiveRecord::Base.connection.select_all(preference_sql).each do |row|
180
+ next unless (key = rule_cache_key row) && (user_id = row['user_id'])
181
+ add_preference_hash_values key, row['rule_id'].to_i, user_id.to_i
165
182
  end
166
183
  end
167
184
 
168
- def all_rule_keys_with_id_for user_id
169
- ActiveRecord::Base.connection.select_all(user_rule_sql(user_id)).each do |row|
170
- if key = cache_key(row)
171
- yield(key, row['rule_id'].to_i)
172
- end
173
- end
185
+ def add_preference_hash_values key, rule_id, user_id
186
+ @rule_hash[preference_key(key, user_id)] = rule_id
187
+ @user_ids_hash[key] ||= []
188
+ @user_ids_hash[key] << user_id
189
+ @rule_keys_hash[user_id] ||= []
190
+ @rule_keys_hash[user_id] << key
174
191
  end
175
192
 
176
193
  def cached_rule_keys_for user_id
@@ -194,53 +211,48 @@ module ClassMethods
194
211
  end
195
212
  end
196
213
 
197
- def user_rule_cards user_name, setting_code
214
+ def preference_cards user_name, setting_code
198
215
  Card.search(
199
216
  { right: { codename: setting_code },
200
217
  left: { left: { type_id: SetID }, right: user_name }
201
- }, "rule cards for user: #{user_name}"
218
+ }, "rule cards for user: #{user_name}"
202
219
  )
203
220
  end
204
221
 
205
222
  def rule_cache
206
223
  Card.cache.read('RULES') || begin
207
- rule_hash = {}
208
- all_rule_keys_with_id do |key,rule_id|
209
- rule_hash[key] = rule_id
210
- end
224
+ @rule_hash = {}
225
+ @user_ids_hash = {}
226
+ @rule_keys_hash = {}
211
227
 
212
- user_ids_hash = {}
213
- rule_keys_hash = {}
214
- all_user_rule_keys_with_id_and_user_id do |key, rule_id, user_id|
215
- rule_hash[ user_rule_key(key,user_id) ] = rule_id
216
- user_ids_hash[key] ||= []
217
- user_ids_hash[key] << user_id
218
- rule_keys_hash[user_id] ||= []
219
- rule_keys_hash[user_id] << key
220
- end
221
- write_user_ids_cache user_ids_hash
222
- write_rule_keys_cache rule_keys_hash
223
- write_rule_cache rule_hash
228
+ interpret_simple_rules
229
+ interpret_preferences
230
+
231
+ write_user_ids_cache @user_ids_hash
232
+ write_rule_keys_cache @rule_keys_hash
233
+ write_rule_cache @rule_hash
224
234
  end
225
235
  end
226
236
 
227
- def user_rule_key key, user_id
237
+ def preference_key key, user_id
228
238
  "#{key}+#{user_id}"
229
239
  end
230
240
 
231
241
  # all users that have a user-specific rule for a given rule key
232
242
  def user_ids_cache
233
243
  Card.cache.read('USER_IDS') || begin
244
+ clear_rule_cache
234
245
  rule_cache
235
- Card.cache.read('USER_IDS')
246
+ @user_ids_hash
236
247
  end
237
248
  end
238
249
 
239
250
  # all keys of user-specific rules for a given user
240
251
  def rule_keys_cache
241
252
  Card.cache.read('RULE_KEYS') || begin
253
+ clear_rule_cache
242
254
  rule_cache
243
- Card.cache.read('RULE_KEYS')
255
+ @rule_keys_hash
244
256
  end
245
257
  end
246
258
 
@@ -250,34 +262,11 @@ module ClassMethods
250
262
  write_rule_keys_cache nil
251
263
  end
252
264
 
253
- def clear_user_rule_cache
265
+ def clear_preference_cache
266
+ # FIXME: too entwined!
254
267
  clear_rule_cache
255
268
  end
256
269
 
257
- def refresh_rule_cache_for_user user_id
258
- rule_hash = rule_cache
259
- user_ids_hash = user_ids_cache
260
- rule_keys_hash = rule_keys_cache
261
-
262
- cached_rule_keys_for(user_id).each do |key|
263
- rule_hash[ user_rule_key(key, user_id) ] = nil
264
- user_ids_hash[ key ].delete(user_id)
265
- end
266
- rule_keys_hash[ user_id ] = nil
267
-
268
- all_rule_keys_with_id_for(user_id) do |key, rule_id|
269
- rule_hash[ user_rule_key(key,user_id) ] = rule_id
270
-
271
- user_ids_hash[ key ] ||= []
272
- user_ids_hash[ key ] << user_id
273
- rule_keys_hash[ user_id ] ||= []
274
- rule_keys_hash[ user_id ] << key
275
- end
276
- write_user_ids_cache user_ids_hash
277
- write_rule_keys_cache rule_keys_hash
278
- write_rule_cache rule_hash
279
- end
280
-
281
270
  def write_rule_cache hash
282
271
  Card.cache.write 'RULES', hash
283
272
  end
@@ -293,8 +282,8 @@ module ClassMethods
293
282
  def read_rule_cache
294
283
  Card.cache.read('READRULES') || begin
295
284
  hash = {}
296
- ActiveRecord::Base.connection.select_all(
297
- Card::Set::All::Rules::ReadRuleSQL
285
+ Card.connection.select_all(
286
+ Card::Set::All::Rules::READ_RULE_SQL
298
287
  ).each do |row|
299
288
  party_id = row['party_id'].to_i
300
289
  hash[party_id] ||= []
@@ -20,7 +20,7 @@ event :cache_expired_for_name_change, before: :store, on: :update,
20
20
  Card.follow_caches_expired
21
21
  end
22
22
 
23
- event :cache_expired_for_new_user_rule,
23
+ event :cache_expired_for_new_preference,
24
24
  before: :extend,
25
25
  when: proc { |c| c.follow_rule_card? } do
26
26
  Card.follow_caches_expired
@@ -95,7 +95,7 @@ def follower_names
95
95
  end
96
96
 
97
97
  def follow_rule_card?
98
- is_user_rule? && rule_setting_name == '*follow'
98
+ is_preference? && rule_setting_name == '*follow'
99
99
  end
100
100
 
101
101
  def follow_option?
@@ -236,7 +236,7 @@ end
236
236
  module ClassMethods
237
237
  def follow_caches_expired
238
238
  Card.clear_follower_ids_cache
239
- Card.clear_user_rule_cache
239
+ Card.clear_preference_cache
240
240
  end
241
241
 
242
242
  def follower_ids_cache
@@ -5,7 +5,7 @@ def raw_content
5
5
  @raw_content ||= if left
6
6
  items = if left.type_id == Card::UserID
7
7
  user = left
8
- follow_rules = Card.user_rule_cards left.name, 'follow'
8
+ follow_rules = Card.preference_cards left.name, 'follow'
9
9
  follow_rules.map {|card| "[[#{card.name}]]" }
10
10
  end.join "\n"
11
11
  else
@@ -11,7 +11,7 @@ def rule_set_key
11
11
  end
12
12
 
13
13
  def rule_set_name
14
- if is_user_rule?
14
+ if is_preference?
15
15
  cardname.trunk_name.trunk_name
16
16
  else
17
17
  cardname.trunk_name
@@ -19,7 +19,7 @@ def rule_set_name
19
19
  end
20
20
 
21
21
  def rule_set
22
- if is_user_rule?
22
+ if is_preference?
23
23
  self[0..-3]
24
24
  else
25
25
  trunk
@@ -31,7 +31,7 @@ def rule_setting_name
31
31
  end
32
32
 
33
33
  def rule_user_setting_name
34
- if is_user_rule?
34
+ if is_preference?
35
35
  "#{rule_user_name}+#{rule_setting_name}"
36
36
  else
37
37
  rule_setting_name
@@ -39,11 +39,11 @@ def rule_user_setting_name
39
39
  end
40
40
 
41
41
  def rule_user_name
42
- is_user_rule? ? cardname.trunk_name.tag : nil
42
+ is_preference? ? cardname.trunk_name.tag : nil
43
43
  end
44
44
 
45
45
  def rule_user
46
- is_user_rule? ? self[-2] : nil
46
+ is_preference? ? self[-2] : nil
47
47
  end
48
48
 
49
49
  # ~~~~~~~~~~ determine the set options to which the user can apply the rule.
@@ -92,7 +92,7 @@ def set_options
92
92
  end
93
93
 
94
94
  def set_prototype
95
- if is_user_rule?
95
+ if is_preference?
96
96
  self[0..-3].prototype
97
97
  else
98
98
  trunk.prototype
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: card
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.17.3
4
+ version: 1.17.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ethan McCutchen
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2016-02-10 00:00:00.000000000 Z
14
+ date: 2016-02-13 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: smartname
@@ -1196,7 +1196,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
1196
1196
  version: '0'
1197
1197
  requirements: []
1198
1198
  rubyforge_project:
1199
- rubygems_version: 2.4.8
1199
+ rubygems_version: 2.5.1
1200
1200
  signing_key:
1201
1201
  specification_version: 4
1202
1202
  summary: an atomic, set-driven content engine
@@ -1226,4 +1226,3 @@ test_files:
1226
1226
  - spec/models/card/validation_spec.rb
1227
1227
  - spec/models/card_spec.rb
1228
1228
  - spec/spec_helper.rb
1229
- has_rdoc: