heimdallr-resource 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "heimdallr-resource"
6
- s.version = "1.0.2"
6
+ s.version = "1.0.3"
7
7
  s.authors = ["Peter Zotov", "Boris Staal"]
8
8
  s.email = ["whitequark@whitequark.org", "boris@roundlake.ru"]
9
9
  s.homepage = "http://github.com/roundlake/heimdallr-resource"
@@ -21,17 +21,17 @@ module Heimdallr
21
21
 
22
22
  if target
23
23
  if options[:singleton]
24
- scope = target.send(:"#{options[:resource].parameterize('_')}")
24
+ scope = target.send(:"#{variable_name(options)}")
25
25
  else
26
- scope = target.send(:"#{options[:resource].parameterize('_').pluralize}")
26
+ scope = target.send(:"#{variable_name(options).pluralize}")
27
27
  end
28
28
  elsif options[:shallow]
29
- scope = options[:resource].classify.constantize.scoped
29
+ scope = class_name(options).constantize.scoped
30
30
  else
31
31
  raise "Cannot fetch #{options[:resource]} via #{options[:through]}"
32
32
  end
33
33
  else
34
- scope = options[:resource].classify.constantize.scoped
34
+ scope = class_name(options).constantize.scoped
35
35
  end
36
36
 
37
37
  loaders = {
@@ -40,20 +40,25 @@ module Heimdallr
40
40
  },
41
41
 
42
42
  new_record: -> {
43
- controller.instance_variable_set(ivar_name(controller, options),
44
- scope.new(controller.params[options[:resource].split('/').last]))
43
+ controller.instance_variable_set(
44
+ ivar_name(controller, options),
45
+ scope.new(controller.params[params_key_name(options)] || {})
46
+ )
45
47
  },
46
48
 
47
49
  record: -> {
48
- controller.instance_variable_set(ivar_name(controller, options),
49
- scope.find(controller.params[:"#{options[:resource]}_id"] ||
50
- controller.params[:id]))
50
+ controller.instance_variable_set(
51
+ ivar_name(controller, options),
52
+ scope.find([:"#{params_key_name(options)}_id", :id].map{|key| controller.params[key] }.reject(&:blank?)[0])
53
+ )
51
54
  },
52
55
 
53
56
  related_record: -> {
54
- if controller.params[:"#{options[:resource]}_id"]
55
- controller.instance_variable_set(ivar_name(controller, options),
56
- scope.find(controller.params[:"#{options[:resource]}_id"]))
57
+ unless controller.params[:"#{params_key_name(options)}_id"].blank?
58
+ controller.instance_variable_set(
59
+ ivar_name(controller, options),
60
+ scope.find(controller.params[:"#{params_key_name(options)}_id"])
61
+ )
57
62
  end
58
63
  }
59
64
  }
@@ -95,23 +100,23 @@ module Heimdallr
95
100
 
96
101
  def load_target(controller, options)
97
102
  Array.wrap(options[:through]).map do |parent|
98
- loaded = controller.instance_variable_get(:"@#{parent}")
103
+ loaded = controller.instance_variable_get(:"@#{variable_name parent}")
99
104
  unless loaded
100
105
  load(controller, :resource => parent.to_s, :related => true)
101
- loaded = controller.instance_variable_get(:"@#{parent}")
106
+ loaded = controller.instance_variable_get(:"@#{variable_name parent}")
102
107
  end
103
108
  if loaded && options[:authorize_chain]
104
109
  authorize(controller, :resource => parent.to_s, :related => true)
105
110
  end
106
- controller.instance_variable_get(:"@#{parent}")
111
+ controller.instance_variable_get(:"@#{variable_name parent}")
107
112
  end.reject(&:nil?).first
108
113
  end
109
114
 
110
115
  def ivar_name(controller, options)
111
116
  if action_type(controller.params[:action], options) == :collection
112
- :"@#{options[:resource].parameterize('_').pluralize}"
117
+ :"@#{variable_name(options).pluralize}"
113
118
  else
114
- :"@#{options[:resource].parameterize('_')}"
119
+ :"@#{variable_name(options)}"
115
120
  end
116
121
  end
117
122
 
@@ -138,6 +143,30 @@ module Heimdallr
138
143
  end
139
144
  end
140
145
  end
146
+
147
+ def variable_name(options)
148
+ if options.kind_of? Hash
149
+ options[:resource]
150
+ else
151
+ options.to_s
152
+ end.parameterize('_')
153
+ end
154
+
155
+ def class_name(options)
156
+ if options.kind_of? Hash
157
+ options[:resource]
158
+ else
159
+ options.to_s
160
+ end.classify
161
+ end
162
+
163
+ def params_key_name(options)
164
+ if options.kind_of? Hash
165
+ options[:resource]
166
+ else
167
+ options.to_s
168
+ end.split('/').last
169
+ end
141
170
  end
142
171
  end
143
172
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: heimdallr-resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-04-12 00:00:00.000000000 Z
13
+ date: 2012-05-27 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec-rails
17
- requirement: &70169121125720 !ruby/object:Gem::Requirement
17
+ requirement: &70177728312580 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *70169121125720
25
+ version_requirements: *70177728312580
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: activerecord
28
- requirement: &70169121125180 !ruby/object:Gem::Requirement
28
+ requirement: &70177728312140 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70169121125180
36
+ version_requirements: *70177728312140
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: sqlite3
39
- requirement: &70169121124440 !ruby/object:Gem::Requirement
39
+ requirement: &70177728311720 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *70169121124440
47
+ version_requirements: *70177728311720
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: tzinfo
50
- requirement: &70169121116540 !ruby/object:Gem::Requirement
50
+ requirement: &70177728311300 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *70169121116540
58
+ version_requirements: *70177728311300
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: heimdallr
61
- requirement: &70169121115860 !ruby/object:Gem::Requirement
61
+ requirement: &70177728310880 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ! '>='
@@ -66,7 +66,7 @@ dependencies:
66
66
  version: '0'
67
67
  type: :runtime
68
68
  prerelease: false
69
- version_requirements: *70169121115860
69
+ version_requirements: *70177728310880
70
70
  description: Heimdallr-Resource provides CanCan-like interface for Heimdallr-secured
71
71
  objects.
72
72
  email: