active_restrictors 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc
CHANGED
@@ -223,34 +223,47 @@ module ActiveRestrictor
|
|
223
223
|
end
|
224
224
|
|
225
225
|
def self.included(klass)
|
226
|
+
patch_base_restrictors(klass) unless klass.instance_methods.include?(:add_restrictor)
|
227
|
+
patch_user_restrictors(klass)
|
228
|
+
end
|
229
|
+
|
230
|
+
def self.patch_base_restrictors(klass)
|
226
231
|
# Patch up the model we have been called on
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
include InstanceMethods
|
232
|
+
klass.class_eval do
|
233
|
+
cattr_accessor :restrictors
|
234
|
+
|
235
|
+
extend ClassMethods
|
236
|
+
include InstanceMethods
|
233
237
|
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
end
|
238
|
+
unless(klass.singleton_methods.map(&:to_sym).include?(:_restrictor_custom_user_class))
|
239
|
+
class << self
|
240
|
+
# This can be overridden for customization
|
241
|
+
def _restrictor_custom_user_class
|
242
|
+
User
|
240
243
|
end
|
241
244
|
end
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
245
|
+
end
|
246
|
+
scope :allowed_for, lambda{|*args|
|
247
|
+
user = args.detect{|item|item.is_a?(User)}
|
248
|
+
if(user.present?)
|
249
|
+
r_scope = user.send("allowed_#{klass.name.tableize}").select("#{klass.table_name}.id")
|
250
|
+
r_scope.arel.ast.cores.first.projections.delete_if{|item| item != "#{klass.table_name}.id"}
|
251
|
+
where("#{table_name}.id IN (#{r_scope.to_sql})")
|
252
|
+
else
|
253
|
+
where('null')
|
254
|
+
end
|
255
|
+
}
|
256
|
+
|
257
|
+
class << self
|
258
|
+
def inherited(klass)
|
259
|
+
ActiveRestrictor.patch_user_restrictors(klass)
|
260
|
+
super
|
261
|
+
end
|
252
262
|
end
|
253
263
|
end
|
264
|
+
end
|
265
|
+
|
266
|
+
def self.patch_user_restrictors(klass)
|
254
267
|
# Patch up the user to provide restricted methods
|
255
268
|
([klass._restrictor_custom_user_class] + klass._restrictor_custom_user_class.descendants).compact.each do |user_klass|
|
256
269
|
user_klass.class_eval do
|
@@ -40,28 +40,42 @@ module ActiveRestrictors
|
|
40
40
|
if(restrictor[:views][:user_values_only].respond_to?(:call))
|
41
41
|
user = restrictor[:views][:user_values_only].call
|
42
42
|
if(user)
|
43
|
-
values = user.send(restrictor[:name].to_sym).
|
43
|
+
values = user.send(restrictor[:name].to_sym).order(restrictor[:value]).all
|
44
44
|
else
|
45
|
-
values = restrictor[:
|
45
|
+
values = restrictor[:value][:user_values_only].send(restrictor[:name].to_sym).order(restrictor[:view][:value]).all
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
49
49
|
@_restrictor_inflector_helper ||= Class.send(:include, ActiveSupport::Inflector).new
|
50
50
|
values = restrictor[:class].find(:all, :order => restrictor[:views][:value]) unless values
|
51
|
-
|
52
|
-
form.
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
:multiple => restrictor[:views][:multiple]
|
51
|
+
if(defined?(Formtastic) && form.is_a?(Formtastic::FormBuilder))
|
52
|
+
form.input(
|
53
|
+
restrictor[:name],
|
54
|
+
:as => :select,
|
55
|
+
:collection => ActiveSupport::OrderedHash[
|
56
|
+
*restrictor[:class].order(restrictor[:views][:value]).all.map{|r|
|
57
|
+
[r.send(restrictor[:views][:value]), r.id]
|
58
|
+
}.flatten
|
59
|
+
],
|
60
|
+
:multiple => restrictor[:views][:multiple],
|
61
|
+
:include_blank => true
|
63
62
|
)
|
64
|
-
|
63
|
+
else
|
64
|
+
[
|
65
|
+
form.label(restrictor[:name]),
|
66
|
+
form.collection_select(
|
67
|
+
"#{@_restrictor_inflector_helper.singularize(restrictor[:name])}_ids",
|
68
|
+
values,
|
69
|
+
restrictor[:views][:id],
|
70
|
+
restrictor[:views][:value],
|
71
|
+
{
|
72
|
+
:include_blank => restrictor[:views][:include_blank],
|
73
|
+
:selected => Array(obj.send(restrictor[:name])).map(&restrictor[:views][:id].to_sym)
|
74
|
+
},
|
75
|
+
:multiple => restrictor[:views][:multiple]
|
76
|
+
)
|
77
|
+
]
|
78
|
+
end
|
65
79
|
end
|
66
80
|
else
|
67
81
|
[]
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_restrictors
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 2
|
10
|
+
version: 0.2.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Chris Roberts
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-02-10 00:00:00 -08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|