decent_exposure 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bb111212d14e0e4556fa3b3503a1a93ba68ea723
4
+ data.tar.gz: d8f9aa40d3934c500b367016efb5f86b8e98a0a8
5
+ SHA512:
6
+ metadata.gz: e5ccc36891dc074355e8d7c01e0fe0c1ff2e244371ba60dc2d8076095158cd32a64679a58b3291989c458c9f9a2162fbd61dac616b916cf0752c42a94b36112a
7
+ data.tar.gz: e0a369915128c353be4d1671f711a4aaffec0330d81cdef76da77dd62356f982e422cf9cbe05363465224490ffcb8acfdc92f43fad06615267a7f56894450282
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Gem Version](https://badge.fury.io/rb/decent_exposure.png)](http://badge.fury.io/rb/decent_exposure) [![Build Status](https://api.travis-ci.org/voxdolo/decent_exposure.png?branch=master)](https://travis-ci.org/voxdolo/decent_exposure) [![Code Climate](https://codeclimate.com/github/voxdolo/decent_exposure.png)](https://codeclimate.com/github/voxdolo/decent_exposure)
2
+
1
3
  ## Mad Decent
2
4
 
3
5
  Rails controllers are the sweaty armpit of every rails app. This is due, in
@@ -76,7 +78,7 @@ handling all that repetitive initialization, as we'll see next.
76
78
 
77
79
  Even if you decide not to use `decent_exposure`, do yourself a favor and stop
78
80
  using instance variables in your views. Your code will be cleaner and easier to
79
- refactor as a result. If you want to learn more about his approach, I've
81
+ refactor as a result. If you want to learn more about this approach, I've
80
82
  expanded on my thoughts in the article [A Diatribe on Maintaining State][1].
81
83
 
82
84
  ## Environmental Awareness
@@ -10,7 +10,7 @@ module DecentExposure
10
10
  end
11
11
 
12
12
  def scope
13
- if options[:ancestor]
13
+ @scope ||= if options[:ancestor]
14
14
  ancestor_scope
15
15
  else
16
16
  default_scope
@@ -38,7 +38,8 @@ module DecentExposure
38
38
  end
39
39
 
40
40
  def collection_resource
41
- scope.scoped
41
+ return scope if scope.respond_to?(:each)
42
+ scope.send(scope_method)
42
43
  end
43
44
 
44
45
  def id
@@ -64,5 +65,15 @@ module DecentExposure
64
65
  singular_resource
65
66
  end
66
67
  end
68
+
69
+ private
70
+
71
+ def scope_method
72
+ if defined?(ActiveRecord) && ActiveRecord::VERSION::MAJOR > 3
73
+ :all
74
+ else
75
+ :scoped
76
+ end
77
+ end
67
78
  end
68
79
  end
@@ -10,7 +10,7 @@ module DecentExposure
10
10
  end
11
11
 
12
12
  def attributes
13
- params[inflector.singular] || {}
13
+ params[options[:param_key] || inflector.param_key] || {}
14
14
  end
15
15
 
16
16
  def assign_attributes?
@@ -0,0 +1,32 @@
1
+ require 'active_support/inflector'
2
+ require 'active_support/core_ext/string'
3
+
4
+ module DecentExposure
5
+ class ConstantResolver
6
+ attr_reader :context, :constant_name
7
+ def initialize(constant_name, context=Object)
8
+ @context, @constant_name = context, constant_name.classify
9
+ end
10
+
11
+ def constant
12
+ immediate_child || namespace_qualified
13
+ end
14
+
15
+ private
16
+
17
+ def immediate_child
18
+ context.constants.map do |c|
19
+ context.const_get(c) if c.to_s == constant_name
20
+ end.compact.first
21
+ end
22
+
23
+ def namespace_qualified
24
+ namespace.const_get(constant_name)
25
+ end
26
+
27
+ def namespace
28
+ path = context.to_s
29
+ path[0...(path.rindex('::') || 0)].constantize
30
+ end
31
+ end
32
+ end
@@ -2,16 +2,16 @@ require 'decent_exposure/inflector'
2
2
 
3
3
  module DecentExposure
4
4
  class Exposure
5
- attr_accessor :inflector, :strategy, :options
5
+ attr_accessor :name, :strategy, :options
6
6
 
7
7
  def initialize(name, strategy, options)
8
+ self.name = name.to_s
8
9
  self.strategy = strategy
9
10
  self.options = options
10
- self.inflector = DecentExposure::Inflector.new(name, options[:model])
11
11
  end
12
12
 
13
13
  def call(controller)
14
- strategy.new(controller, inflector, options).resource
14
+ strategy.new(controller, name, options).resource
15
15
  end
16
16
  end
17
17
  end
@@ -1,38 +1,32 @@
1
1
  require 'active_support/inflector'
2
2
  require 'active_support/core_ext/string'
3
+ require 'active_model/naming'
3
4
 
4
5
  module DecentExposure
5
6
  class Inflector
6
- attr_reader :string, :original, :model
7
- alias name string
7
+ attr_reader :original, :model
8
8
 
9
- def initialize(name, model=nil)
9
+ def initialize(name, model)
10
10
  @original = name.to_s
11
11
  @model = model
12
- @string = (model || name).to_s.demodulize
13
12
  end
14
13
 
15
- def constant(context=Object)
16
- case model
17
- when Module, Class
18
- model
19
- else
20
- ConstantResolver.new(context, string.classify).constant
21
- end
22
- end
14
+ alias name original
23
15
 
24
- def parameter
25
- singular + "_id"
16
+ def model_name
17
+ @model_name ||= if model.respond_to?(:model_name)
18
+ model.model_name
19
+ else
20
+ ActiveModel::Name.new(model)
21
+ end
26
22
  end
27
23
 
28
- def singular
29
- @singular ||= string.singularize.parameterize
30
- end
24
+ delegate :singular, :plural, :param_key, :to => :model_name
25
+ alias collection plural
31
26
 
32
- def plural
33
- string.pluralize
27
+ def parameter
28
+ "#{model_name.singular}_id"
34
29
  end
35
- alias collection plural
36
30
 
37
31
  def plural?
38
32
  original.pluralize == original && !uncountable?
@@ -41,31 +35,5 @@ module DecentExposure
41
35
  def uncountable?
42
36
  original.pluralize == original.singularize
43
37
  end
44
-
45
- private
46
-
47
- ConstantResolver = Struct.new :context, :constant_name do
48
-
49
- def constant
50
- immediate_child || namespace_qualified
51
- end
52
-
53
- private
54
-
55
- def immediate_child
56
- context.constants.map do |c|
57
- context.const_get(c) if c.to_s == constant_name
58
- end.compact.first
59
- end
60
-
61
- def namespace_qualified
62
- namespace.const_get(constant_name)
63
- end
64
-
65
- def namespace
66
- path = context.to_s
67
- path[0...(path.rindex('::') || 0)].constantize
68
- end
69
- end
70
38
  end
71
39
  end
@@ -9,7 +9,7 @@ module DecentExposure
9
9
  def initialize(name, options={})
10
10
  self.name = name
11
11
  self.custom_strategy_class = options.delete(:strategy)
12
- self.options = options
12
+ self.options = options.merge(:name => name)
13
13
  self.block = Proc.new if block_given?
14
14
  end
15
15
 
@@ -1,13 +1,13 @@
1
+ require 'decent_exposure/inflector'
2
+ require 'decent_exposure/constant_resolver'
3
+
1
4
  module DecentExposure
2
5
  class Strategy
3
- attr_reader :controller, :inflector, :options
4
-
5
- def initialize(controller, inflector, options={})
6
- @controller, @inflector, @options = controller, inflector, options
7
- end
6
+ attr_reader :controller, :name, :options
7
+ attr_writer :model, :inflector
8
8
 
9
- def name
10
- inflector.name
9
+ def initialize(controller, name, options={})
10
+ @controller, @name, @options = controller, name.to_s, options
11
11
  end
12
12
 
13
13
  def resource
@@ -16,8 +16,18 @@ module DecentExposure
16
16
 
17
17
  protected
18
18
 
19
+ def inflector
20
+ @inflector ||= DecentExposure::Inflector.new(name, model)
21
+ end
22
+
19
23
  def model
20
- inflector.constant(controller.class)
24
+ @model ||= case options[:model]
25
+ when Class, Module
26
+ options[:model]
27
+ else
28
+ name_or_model = options[:model] || name
29
+ DecentExposure::ConstantResolver.new(name_or_model.to_s, controller.class).constant
30
+ end
21
31
  end
22
32
 
23
33
  def params
@@ -1,3 +1,3 @@
1
1
  module DecentExposure #:nodoc
2
- VERSION = "2.1.0"
2
+ VERSION = "2.2.0"
3
3
  end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decent_exposure
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 2.1.0
4
+ version: 2.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Stephen Caudill
@@ -11,73 +10,65 @@ authors:
11
10
  autorequire:
12
11
  bindir: bin
13
12
  cert_chain: []
14
- date: 2013-02-12 00:00:00.000000000 Z
13
+ date: 2013-05-24 00:00:00.000000000 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
- version_requirements: !ruby/object:Gem::Requirement
16
+ name: rspec
17
+ requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
21
  version: '2.11'
22
- none: false
23
- name: rspec
24
22
  type: :development
25
23
  prerelease: false
26
- requirement: !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
27
25
  requirements:
28
26
  - - ~>
29
27
  - !ruby/object:Gem::Version
30
28
  version: '2.11'
31
- none: false
32
29
  - !ruby/object:Gem::Dependency
33
- version_requirements: !ruby/object:Gem::Requirement
30
+ name: rspec-rails
31
+ requirement: !ruby/object:Gem::Requirement
34
32
  requirements:
35
33
  - - ~>
36
34
  - !ruby/object:Gem::Version
37
35
  version: '2.11'
38
- none: false
39
- name: rspec-rails
40
36
  type: :development
41
37
  prerelease: false
42
- requirement: !ruby/object:Gem::Requirement
38
+ version_requirements: !ruby/object:Gem::Requirement
43
39
  requirements:
44
40
  - - ~>
45
41
  - !ruby/object:Gem::Version
46
42
  version: '2.11'
47
- none: false
48
43
  - !ruby/object:Gem::Dependency
49
- version_requirements: !ruby/object:Gem::Requirement
44
+ name: actionpack
45
+ requirement: !ruby/object:Gem::Requirement
50
46
  requirements:
51
- - - ! '>='
47
+ - - '>='
52
48
  - !ruby/object:Gem::Version
53
49
  version: 3.1.0
54
- none: false
55
- name: actionpack
56
50
  type: :development
57
51
  prerelease: false
58
- requirement: !ruby/object:Gem::Requirement
52
+ version_requirements: !ruby/object:Gem::Requirement
59
53
  requirements:
60
- - - ! '>='
54
+ - - '>='
61
55
  - !ruby/object:Gem::Version
62
56
  version: 3.1.0
63
- none: false
64
57
  - !ruby/object:Gem::Dependency
65
- version_requirements: !ruby/object:Gem::Requirement
58
+ name: activesupport
59
+ requirement: !ruby/object:Gem::Requirement
66
60
  requirements:
67
- - - ! '>='
61
+ - - '>='
68
62
  - !ruby/object:Gem::Version
69
63
  version: 3.1.0
70
- none: false
71
- name: activesupport
72
64
  type: :development
73
65
  prerelease: false
74
- requirement: !ruby/object:Gem::Requirement
66
+ version_requirements: !ruby/object:Gem::Requirement
75
67
  requirements:
76
- - - ! '>='
68
+ - - '>='
77
69
  - !ruby/object:Gem::Version
78
70
  version: 3.1.0
79
- none: false
80
- description: ! "\n DecentExposure helps you program to an interface, rather than
71
+ description: "\n DecentExposure helps you program to an interface, rather than
81
72
  an\n implementation in your Rails controllers. The fact of the matter is that\n
82
73
  \ sharing state via instance variables in controllers promotes close coupling\n
83
74
  \ with views. DecentExposure gives you a declarative manner of exposing an\n
@@ -91,6 +82,7 @@ files:
91
82
  - lib/decent_exposure/active_record_strategy.rb
92
83
  - lib/decent_exposure/active_record_with_eager_attributes_strategy.rb
93
84
  - lib/decent_exposure/configuration.rb
85
+ - lib/decent_exposure/constant_resolver.rb
94
86
  - lib/decent_exposure/expose.rb
95
87
  - lib/decent_exposure/exposure.rb
96
88
  - lib/decent_exposure/inflector.rb
@@ -102,6 +94,7 @@ files:
102
94
  - README.md
103
95
  homepage: http://github.com/voxdolo/decent_exposure
104
96
  licenses: []
97
+ metadata: {}
105
98
  post_install_message:
106
99
  rdoc_options:
107
100
  - --charset=UTF-8
@@ -109,20 +102,18 @@ require_paths:
109
102
  - lib
110
103
  required_ruby_version: !ruby/object:Gem::Requirement
111
104
  requirements:
112
- - - ! '>='
105
+ - - '>='
113
106
  - !ruby/object:Gem::Version
114
107
  version: '0'
115
- none: false
116
108
  required_rubygems_version: !ruby/object:Gem::Requirement
117
109
  requirements:
118
- - - ! '>='
110
+ - - '>='
119
111
  - !ruby/object:Gem::Version
120
112
  version: 1.3.6
121
- none: false
122
113
  requirements: []
123
114
  rubyforge_project:
124
- rubygems_version: 1.8.24
115
+ rubygems_version: 2.0.0
125
116
  signing_key:
126
- specification_version: 3
117
+ specification_version: 4
127
118
  summary: A helper for creating declarative interfaces in controllers
128
119
  test_files: []