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
@@ -1,3 +1,7 @@
1
+ == v0.2.2
2
+ * Added support for formtastic to view helpers
3
+ * Cleaned up dynamic method generation
4
+
1
5
  == v0.2.1
2
6
  * Restrictors not added if table does not exist (allows restrictors to be defined before migration has setup model)
3
7
 
@@ -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
- ([klass] + klass.descendants).compact.each do |base|
228
- base.class_eval do
229
- cattr_accessor :restrictors
230
-
231
- extend ClassMethods
232
- include InstanceMethods
232
+ klass.class_eval do
233
+ cattr_accessor :restrictors
234
+
235
+ extend ClassMethods
236
+ include InstanceMethods
233
237
 
234
- unless(base.singleton_methods.map(&:to_sym).include?(:_restrictor_custom_user_class))
235
- class << self
236
- # This can be overridden for customization
237
- def _restrictor_custom_user_class
238
- User
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
- scope :allowed_for, lambda{|*args|
243
- user = args.detect{|item|item.is_a?(User)}
244
- if(user.present?)
245
- r_scope = user.send("allowed_#{base.name.tableize}").select("#{base.table_name}.id")
246
- r_scope.arel.ast.cores.first.projections.delete_if{|item| item != "#{base.table_name}.id"}
247
- where("#{table_name}.id IN (#{r_scope.to_sql})")
248
- else
249
- where('null')
250
- end
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).find(:all, :order => restrictor[:value])
43
+ values = user.send(restrictor[:name].to_sym).order(restrictor[:value]).all
44
44
  else
45
- values = restrictor[:views][:user_values_only].send(restrictor[:name].to_sym).find(:all, :order => restrictor[:views][:value])
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.label(restrictor[:name]),
53
- form.collection_select(
54
- "#{@_restrictor_inflector_helper.singularize(restrictor[:name])}_ids",
55
- values,
56
- restrictor[:views][:id],
57
- restrictor[:views][:value],
58
- {
59
- :include_blank => restrictor[:views][:include_blank],
60
- :selected => Array(obj.send(restrictor[:name])).map(&restrictor[:views][:id].to_sym)
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
  []
@@ -13,5 +13,5 @@ module ActiveRestrictors
13
13
  end
14
14
  end
15
15
 
16
- VERSION = Version.new('0.2.1')
16
+ VERSION = Version.new('0.2.2')
17
17
  end
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: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
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-01-27 00:00:00 -08:00
18
+ date: 2012-02-10 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency