light-decorator 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,59 @@
1
+ module Light
2
+ module Decorator
3
+ module Concerns
4
+ module Relation
5
+ extend ActiveSupport::Concern
6
+
7
+ # Decorate ActiveRecord::Relation
8
+ #
9
+ # @param [Hash] options
10
+ # @return [ActiveRecord::Relation] decorated collection
11
+ def decorate(options = {})
12
+ @decorated = true
13
+
14
+ override_exec_queries(options)
15
+
16
+ self
17
+ end
18
+
19
+ # Check current ActiveRecord::Relation is decorated or not
20
+ #
21
+ # @return [Bool]
22
+ def decorated?
23
+ !@decorated.nil?
24
+ end
25
+
26
+ private
27
+
28
+ def override_exec_queries(options)
29
+ define_singleton_method :exec_queries do
30
+ super()
31
+
32
+ @records = @records.map do |record|
33
+ decorate_associations(record, options.reverse_merge(soft: true))
34
+ record.decorate(options)
35
+ end
36
+ end
37
+ end
38
+
39
+ def decorate_associations(record, options)
40
+ record.instance_variable_get(:@association_cache).each do |_, association|
41
+ next if association.inversed || association.target.blank?
42
+
43
+ if association.target.is_a?(Array)
44
+ targets = association.target.map do |target|
45
+ decorate_associations(target, options)
46
+ target.decorate(options)
47
+ end
48
+
49
+ association.instance_variable_set(:@target, targets)
50
+ else
51
+ decorate_associations(association.target, options)
52
+ association.instance_variable_set(:@target, association.target.decorate(options))
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,71 @@
1
+ module Light
2
+ module Decorator
3
+ class Decorate
4
+ # @return original object
5
+ attr_reader :object
6
+
7
+ # Decorate the object
8
+ #
9
+ # @param [Object] object for decoration
10
+ # @param [Hash] options
11
+ #
12
+ # @return [Object] decorated object
13
+ def initialize(object, options = {})
14
+ @object = object
15
+ @options = options
16
+
17
+ delegate_methods
18
+ decorate_associations
19
+ end
20
+
21
+ # Check current ActiveRecord::Model is decorated or not
22
+ #
23
+ # @return [Bool]
24
+ def decorated?
25
+ true
26
+ end
27
+
28
+ # Current view scope
29
+ #
30
+ # @return [ActionView::Base]
31
+ def helpers
32
+ return @helpers if defined?(@helpers)
33
+ @helpers = Light::Decorator::ViewContext.current
34
+ end
35
+
36
+ def ==(other)
37
+ super || object == other
38
+ end
39
+
40
+ def eql?(other)
41
+ super || object.eql?(other)
42
+ end
43
+
44
+ class << self
45
+ alias decorate new
46
+ end
47
+
48
+ alias o object
49
+ alias h helpers
50
+
51
+ private
52
+
53
+ def delegate_methods
54
+ # It's more faster than using Forwardable
55
+ (object.public_methods - Light::Decorator::NOT_DELEGATABLE_METHODS).each do |method|
56
+ define_singleton_method method do |*args, &block|
57
+ object.__send__(method, *args, &block)
58
+ end
59
+ end
60
+ end
61
+
62
+ def decorate_associations
63
+ object.class.reflect_on_all_associations.map(&:name).each do |reflection_name|
64
+ define_singleton_method reflection_name do
65
+ object.public_send(reflection_name).decorate(@options.reverse_merge(soft: true))
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,6 @@
1
+ module Light
2
+ module Decorator
3
+ class Error < StandardError; end
4
+ class NotFound < Light::Decorator::Error; end
5
+ end
6
+ end
@@ -0,0 +1,28 @@
1
+ module Light
2
+ module Decorator
3
+ class Railtie < Rails::Railtie
4
+ config.after_initialize do |app|
5
+ # Eager loading of Decorators classes
6
+ app.config.paths.add 'app/decorators', eager_load: true
7
+ end
8
+
9
+ initializer 'light.decorator.inject_orm' do
10
+ ActiveSupport.on_load :active_record do
11
+ ActiveRecord::Base.send(:include, ::Light::Decorator::Concerns::Base)
12
+ ActiveRecord::Relation.send(:include, ::Light::Decorator::Concerns::Relation)
13
+ ActiveRecord::Associations::CollectionProxy.send(
14
+ :include, ::Light::Decorator::Concerns::Associations::CollectionProxy
15
+ )
16
+ end
17
+ end
18
+
19
+ initializer 'light.decorator.load_view_context' do
20
+ [:action_controller, :action_mailer].each do |action_module|
21
+ ActiveSupport.on_load action_module do
22
+ include ::Light::Decorator::ViewContext
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,5 @@
1
+ module Light
2
+ module Decorator
3
+ VERSION = '0.5.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ module Light
2
+ module Decorator
3
+ module ViewContext
4
+ def view_context
5
+ super.tap do |context|
6
+ RequestStore[:light_decorator_context] = context
7
+ end
8
+ end
9
+
10
+ def self.current
11
+ RequestStore[:light_decorator_context] || fake_context
12
+ end
13
+
14
+ # @private
15
+ def self.fake_context
16
+ ActionView::Base.new
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'light/decorator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'light-decorator'
8
+ spec.version = Light::Decorator::VERSION
9
+ spec.authors = ['Andrew Emelianenko']
10
+ spec.email = ['emelianenko.web@gmail.com']
11
+
12
+ spec.summary = 'Light pattern Decorator for Rails'
13
+ spec.description = 'Light pattern Decorator for Rails from Light Ruby'
14
+ spec.homepage = 'https://github.com/light-ruby/light-decorator'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`
18
+ .split("\x0")
19
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'rails', '>= 4.0.0'
26
+ spec.add_dependency 'request_store', '>= 1.0.0'
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.12'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'sqlite3', '~> 1.3.11'
31
+ spec.add_development_dependency 'combustion', '~> 0.5.4'
32
+ spec.add_development_dependency 'appraisal', '~> 2.1'
33
+ spec.add_development_dependency 'rspec', '~> 3.0'
34
+ spec.add_development_dependency 'simplecov', '~> 0.11.2'
35
+ spec.add_development_dependency 'capybara', '~> 2.7.0'
36
+ spec.add_development_dependency 'factory_girl', '~> 4.0'
37
+ spec.add_development_dependency 'ffaker', '~> 2.2.0'
38
+ spec.add_development_dependency 'codeclimate-test-reporter'
39
+ end
metadata ADDED
@@ -0,0 +1,259 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: light-decorator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Emelianenko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: request_store
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.3.11
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.3.11
83
+ - !ruby/object:Gem::Dependency
84
+ name: combustion
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.5.4
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.5.4
97
+ - !ruby/object:Gem::Dependency
98
+ name: appraisal
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.1'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.1'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.11.2
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.11.2
139
+ - !ruby/object:Gem::Dependency
140
+ name: capybara
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 2.7.0
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 2.7.0
153
+ - !ruby/object:Gem::Dependency
154
+ name: factory_girl
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '4.0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '4.0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: ffaker
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: 2.2.0
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: 2.2.0
181
+ - !ruby/object:Gem::Dependency
182
+ name: codeclimate-test-reporter
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ description: Light pattern Decorator for Rails from Light Ruby
196
+ email:
197
+ - emelianenko.web@gmail.com
198
+ executables: []
199
+ extensions: []
200
+ extra_rdoc_files: []
201
+ files:
202
+ - ".codeclimate.yml"
203
+ - ".gitignore"
204
+ - ".rspec"
205
+ - ".rubocop.yml"
206
+ - ".travis.yml"
207
+ - Appraisals
208
+ - CODE_OF_CONDUCT.md
209
+ - Gemfile
210
+ - LICENSE.txt
211
+ - README.md
212
+ - Rakefile
213
+ - bin/console
214
+ - bin/setup
215
+ - config.ru
216
+ - gemfiles/rails_4_0.gemfile
217
+ - gemfiles/rails_4_0.gemfile.lock
218
+ - gemfiles/rails_4_1.gemfile
219
+ - gemfiles/rails_4_1.gemfile.lock
220
+ - gemfiles/rails_4_2.gemfile
221
+ - gemfiles/rails_4_2.gemfile.lock
222
+ - gemfiles/rails_5_0.gemfile
223
+ - gemfiles/rails_5_0.gemfile.lock
224
+ - lib/light/decorator.rb
225
+ - lib/light/decorator/concerns/associations/collection_proxy.rb
226
+ - lib/light/decorator/concerns/base.rb
227
+ - lib/light/decorator/concerns/relation.rb
228
+ - lib/light/decorator/decorate.rb
229
+ - lib/light/decorator/exceptions.rb
230
+ - lib/light/decorator/railtie.rb
231
+ - lib/light/decorator/version.rb
232
+ - lib/light/decorator/view_context.rb
233
+ - light-decorator.gemspec
234
+ homepage: https://github.com/light-ruby/light-decorator
235
+ licenses:
236
+ - MIT
237
+ metadata: {}
238
+ post_install_message:
239
+ rdoc_options: []
240
+ require_paths:
241
+ - lib
242
+ required_ruby_version: !ruby/object:Gem::Requirement
243
+ requirements:
244
+ - - ">="
245
+ - !ruby/object:Gem::Version
246
+ version: '0'
247
+ required_rubygems_version: !ruby/object:Gem::Requirement
248
+ requirements:
249
+ - - ">="
250
+ - !ruby/object:Gem::Version
251
+ version: '0'
252
+ requirements: []
253
+ rubyforge_project:
254
+ rubygems_version: 2.6.3
255
+ signing_key:
256
+ specification_version: 4
257
+ summary: Light pattern Decorator for Rails
258
+ test_files: []
259
+ has_rdoc: