inherited_resources_helpers 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,55 @@
1
+ module InheritedResources
2
+ module Helpers
3
+ module Accessors
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def define_resource_accessors(controller)
10
+ symbols = parents_symbols + [:self]
11
+ symbols.inject([]) do |parents, name|
12
+ define_resource_accessor(name, parents)
13
+ parents << name
14
+ end
15
+ @resource_accessors_defined = true
16
+ end
17
+
18
+ def define_resource_accessor(name, parents)
19
+ config = resources_configuration[name]
20
+ target = parents.last || config[:parent_class]
21
+ method = config[:finder] || config[:collection_name]
22
+ param = config[:param] || :id
23
+ name = name == :self ? config[:instance_name] : name
24
+
25
+ if target.is_a?(Symbol)
26
+ define_method(name) { send(target).send(method, params[param]).first }
27
+ else
28
+ define_method(name) { target.send(method, params[param]) }
29
+ end
30
+ end
31
+
32
+ def resource_accessors_defined?
33
+ !!@resource_accessors_defined
34
+ end
35
+
36
+ def resource_accessor?(name)
37
+ resource_accessors.include?(name.to_sym)
38
+ end
39
+
40
+ def resource_accessors
41
+ @resource_accessors ||= resources_configuration.except(:polymorphic).values.map { |c| c[:instance_name] }
42
+ end
43
+ end
44
+
45
+ def respond_to?(name)
46
+ self.class.resource_accessor?(name) || super
47
+ end
48
+
49
+ def method_missing(name, *args, &block)
50
+ self.class.define_resource_accessors(self) unless self.class.resource_accessors_defined?
51
+ respond_to?(name) ? send(name) : super
52
+ end
53
+ end
54
+ end
55
+ end
@@ -1,3 +1,3 @@
1
1
  module InheritedResourcesHelpers
2
- VERSION = "0.0.3"
2
+ VERSION = '0.0.4'
3
3
  end
@@ -2,9 +2,10 @@ require 'inherited_resources'
2
2
 
3
3
  module InheritedResources
4
4
  module Helpers
5
+ autoload :Accessors, 'inherited_resources/helpers/accessors'
6
+ autoload :LinkTo, 'inherited_resources/helpers/link_to'
5
7
  autoload :Resources, 'inherited_resources/helpers/resources'
6
8
  autoload :UrlFor, 'inherited_resources/helpers/url_for'
7
- autoload :LinkTo, 'inherited_resources/helpers/link_to'
8
9
  end
9
10
 
10
11
  Base.class_eval do
@@ -12,6 +13,7 @@ module InheritedResources
12
13
  def inherited_with_helpers(base)
13
14
  base.send :include, Helpers::Resources
14
15
  base.send :include, Helpers::UrlFor
16
+ base.send :include, Helpers::Accessors
15
17
  inherited_without_helpers(base)
16
18
  end
17
19
  alias_method_chain :inherited, :helpers # TODO ugh.
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inherited_resources_helpers
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sven Fuchs
@@ -28,6 +28,7 @@ extensions: []
28
28
  extra_rdoc_files: []
29
29
 
30
30
  files:
31
+ - lib/inherited_resources/helpers/accessors.rb
31
32
  - lib/inherited_resources/helpers/link_to.rb
32
33
  - lib/inherited_resources/helpers/resources.rb
33
34
  - lib/inherited_resources/helpers/url_for.rb