record_decorator 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +50 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +135 -0
- data/Rakefile +10 -0
- data/lib/record_decorator.rb +2 -0
- data/lib/record_decorator/decorate.rb +21 -0
- data/lib/record_decorator/decorator.rb +83 -0
- data/lib/record_decorator/railtie.rb +12 -0
- data/lib/record_decorator/version.rb +3 -0
- data/record_decorator.gemspec +23 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 675c2f4d1f9a4bb467068611e3b32a6be1af9349
|
4
|
+
data.tar.gz: adc9b1009f8ec9f184063387f4a7d0ee38345ddf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 18f58ac7f4c49f1782b59e432cc6d65ac735ea4445174dd6ca04a0c3d9c2bac1a11b8ac5374297ae81eee642ed34e820889610677fa7b9e3431b4fff082b8bd6
|
7
|
+
data.tar.gz: 8e553ed4a317e24b6198bae73a79becd64b07c32d1320ae199e7ed2f0aaa11970e5b25d8311ef298c2dcde36504584a257ae3263ba8eecaf8cae57ab43780ed6
|
data/.gitignore
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
# Used by dotenv library to load environment variables.
|
14
|
+
# .env
|
15
|
+
|
16
|
+
## Specific to RubyMotion:
|
17
|
+
.dat*
|
18
|
+
.repl_history
|
19
|
+
build/
|
20
|
+
*.bridgesupport
|
21
|
+
build-iPhoneOS/
|
22
|
+
build-iPhoneSimulator/
|
23
|
+
|
24
|
+
## Specific to RubyMotion (use of CocoaPods):
|
25
|
+
#
|
26
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
27
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
28
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
29
|
+
#
|
30
|
+
# vendor/Pods/
|
31
|
+
|
32
|
+
## Documentation cache and generated files:
|
33
|
+
/.yardoc/
|
34
|
+
/_yardoc/
|
35
|
+
/doc/
|
36
|
+
/rdoc/
|
37
|
+
|
38
|
+
## Environment normalization:
|
39
|
+
/.bundle/
|
40
|
+
/vendor/bundle
|
41
|
+
/lib/bundler/man/
|
42
|
+
|
43
|
+
# for a library or gem, you might want to ignore these files since the code is
|
44
|
+
# intended to run in multiple environments; otherwise, check them in:
|
45
|
+
# Gemfile.lock
|
46
|
+
# .ruby-version
|
47
|
+
# .ruby-gemset
|
48
|
+
|
49
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
50
|
+
.rvmrc
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Aivils Stoss
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
|
2
|
+
## Installation
|
3
|
+
|
4
|
+
Add this line to your application's Gemfile:
|
5
|
+
|
6
|
+
```ruby
|
7
|
+
gem 'record_decorator'
|
8
|
+
```
|
9
|
+
|
10
|
+
## Supported versions ##
|
11
|
+
|
12
|
+
* Rails 3.2.x, 4, 5.0
|
13
|
+
|
14
|
+
## Supported ORMs ##
|
15
|
+
|
16
|
+
ActiveRecord
|
17
|
+
|
18
|
+
If Your ORM is not supported be aware codebase is very small.
|
19
|
+
It works like this:
|
20
|
+
|
21
|
+
```
|
22
|
+
@user = User.find(5)
|
23
|
+
@user.extend User::AddOns::VeryNice
|
24
|
+
```
|
25
|
+
Where
|
26
|
+
@user may be the model or another instance of ruby object
|
27
|
+
User::AddOns::VeryNice is module
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
Simple case
|
32
|
+
|
33
|
+
```
|
34
|
+
#model
|
35
|
+
class User < ActiveRecord::Base
|
36
|
+
end
|
37
|
+
|
38
|
+
# Default decorator
|
39
|
+
# Any decorator uses AR model scope
|
40
|
+
module User::AddOns::Decorator
|
41
|
+
def email
|
42
|
+
"Email: #{super}"
|
43
|
+
end
|
44
|
+
|
45
|
+
def full_name
|
46
|
+
"#{first_name} #{last_name}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# controller must call decorator
|
51
|
+
class UsersController < ApplicationController
|
52
|
+
def index
|
53
|
+
@users = Users.all.page(params[:page]).decorate
|
54
|
+
end
|
55
|
+
|
56
|
+
def show
|
57
|
+
@user = User.find(params[:id]).decorate
|
58
|
+
end
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
Context specific decorators
|
63
|
+
```
|
64
|
+
#model
|
65
|
+
class User < ActiveRecord::Base
|
66
|
+
end
|
67
|
+
|
68
|
+
# Default decorator
|
69
|
+
module User::AddOns::Decorator
|
70
|
+
def full_name
|
71
|
+
"#{first_name} #{last_name}"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
module User::AddOns::Show
|
76
|
+
include User::AddOns::Decorator
|
77
|
+
|
78
|
+
def full_position
|
79
|
+
"#{profile.position}, #{profile.branch}, #{profile.city}"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
module User::AddOns::Step1
|
84
|
+
# add validation for this step
|
85
|
+
def self.extended base
|
86
|
+
base.singleton_class.validates :first_name, :last_name, presence: true
|
87
|
+
end
|
88
|
+
|
89
|
+
# add processing method for this step
|
90
|
+
def process(params, analytic)
|
91
|
+
result = update(permitted_params(params))
|
92
|
+
if result
|
93
|
+
# on success do something
|
94
|
+
analytic.track({user: self, event: 'fill_name'})
|
95
|
+
end
|
96
|
+
result
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
def permitted_params(params)
|
102
|
+
params.require(:user).permit(:first_name, :last_name)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
|
107
|
+
# controller must call decorator
|
108
|
+
class UsersController < ApplicationController
|
109
|
+
def index
|
110
|
+
@users = Users.all.page(params[:page]).decorate
|
111
|
+
end
|
112
|
+
|
113
|
+
def show
|
114
|
+
@user = User.find(params[:id]).decorate(:show)
|
115
|
+
end
|
116
|
+
|
117
|
+
def create_step1
|
118
|
+
@user = User.find(params[:id]).decorate(:step1)
|
119
|
+
# method defined in the decorator Step1
|
120
|
+
@user.process(params, @analytic)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
```
|
124
|
+
|
125
|
+
|
126
|
+
## Contributing
|
127
|
+
|
128
|
+
|
129
|
+
## License
|
130
|
+
|
131
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
132
|
+
|
133
|
+
## Inspired By
|
134
|
+
|
135
|
+
https://github.com/itkrt2y/active_decorator
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'record_decorator/decorator'
|
2
|
+
|
3
|
+
module RecordDecorator
|
4
|
+
module AssociationDecorate
|
5
|
+
def target
|
6
|
+
RecordDecorator::Decorator.decorate_association(owner, super)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module BaseDecorate
|
11
|
+
def decorate(name = nil, options = {})
|
12
|
+
RecordDecorator::Decorator.decorate(self, name, options)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module RelationDecorate
|
17
|
+
def decorate(name = nil, options = {})
|
18
|
+
RecordDecorator::Decorator.decorate(self, name, options)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module RecordDecorator
|
2
|
+
class Decorator
|
3
|
+
class << self
|
4
|
+
def decorate_association(owner, target)
|
5
|
+
if owner.respond_to?(:__record_decorator)
|
6
|
+
decorate target, owner.__record_decorator.name, owner.__record_decorator.options
|
7
|
+
else
|
8
|
+
target
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def decorate(target, name = nil, options = {})
|
13
|
+
return target if defined?(Jbuilder) && Jbuilder === target
|
14
|
+
return target if target.nil?
|
15
|
+
|
16
|
+
if target.is_a?(ActiveRecord::Base)
|
17
|
+
if target.respond_to?(:__record_decorator)
|
18
|
+
return target
|
19
|
+
else
|
20
|
+
target.extend RecordDecorator::Marker
|
21
|
+
target.__record_decorator = OpenStruct.new(name: name, options: options)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
if target.is_a?(Array)
|
26
|
+
target.each do |record|
|
27
|
+
decorate record, name
|
28
|
+
end
|
29
|
+
elsif defined?(ActiveRecord) && target.is_a?(ActiveRecord::Relation) && !target.is_a?(RecordDecorator::RelationDecorator)
|
30
|
+
target.extend RecordDecorator::Marker
|
31
|
+
target.__record_decorator = OpenStruct.new(name: name, options: options)
|
32
|
+
# don't call each nor to_a immediately
|
33
|
+
if target.respond_to?(:records)
|
34
|
+
# Rails 5.0
|
35
|
+
target.extend RecordDecorator::RelationDecorator
|
36
|
+
else
|
37
|
+
# Rails 3.x and 4.x
|
38
|
+
target.extend RecordDecorator::RelationDecoratorLegacy
|
39
|
+
end
|
40
|
+
else
|
41
|
+
decorator = decorator_for target, name
|
42
|
+
puts "Found decorator: '#{decorator}'"
|
43
|
+
return target unless decorator
|
44
|
+
target.extend decorator unless target.is_a? decorator
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def decorator_for(target, name)
|
51
|
+
module_name = "#{target.class.name}::AddOns::#{name ? name.to_s.camelize : 'Decorator'}"
|
52
|
+
puts "Search for decorator: '#{module_name}'"
|
53
|
+
_const_get(module_name)
|
54
|
+
end
|
55
|
+
|
56
|
+
def _const_get(const)
|
57
|
+
const.safe_constantize
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
module Marker
|
63
|
+
def self.extended base
|
64
|
+
base.singleton_class.send(:attr_accessor, :__record_decorator)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
module RelationDecoratorLegacy
|
69
|
+
def to_a
|
70
|
+
super.tap do |arr|
|
71
|
+
RecordDecorator::Decorator.decorate arr, __record_decorator.name, __record_decorator.options
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
module RelationDecorator
|
77
|
+
def records
|
78
|
+
super.tap do |arr|
|
79
|
+
RecordDecorator::Decorator.decorate arr, __record_decorator.name, __record_decorator.options
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'record_decorator/decorate'
|
3
|
+
|
4
|
+
module RecordDecorator
|
5
|
+
class Railtie < ::Rails::Railtie
|
6
|
+
initializer 'record_decorator' do
|
7
|
+
ActiveRecord::Associations::Association.prepend RecordDecorator::AssociationDecorate
|
8
|
+
ActiveRecord::Base.include RecordDecorator::BaseDecorate
|
9
|
+
ActiveRecord::Relation.include RecordDecorator::RelationDecorate
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'record_decorator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "record_decorator"
|
8
|
+
spec.version = RecordDecorator::VERSION
|
9
|
+
spec.author = "Aivils Stoss"
|
10
|
+
spec.email = "aivils@ithouse.lv"
|
11
|
+
|
12
|
+
spec.summary = "decorate active record and his associations with anything"
|
13
|
+
spec.description = ""
|
14
|
+
spec.homepage = ""
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "rails", ">= 3.2"
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: record_decorator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aivils Stoss
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-14 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: '3.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: ''
|
56
|
+
email: aivils@ithouse.lv
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- ".gitignore"
|
62
|
+
- Gemfile
|
63
|
+
- LICENSE.txt
|
64
|
+
- README.md
|
65
|
+
- Rakefile
|
66
|
+
- lib/record_decorator.rb
|
67
|
+
- lib/record_decorator/decorate.rb
|
68
|
+
- lib/record_decorator/decorator.rb
|
69
|
+
- lib/record_decorator/railtie.rb
|
70
|
+
- lib/record_decorator/version.rb
|
71
|
+
- record_decorator.gemspec
|
72
|
+
homepage: ''
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.6.10
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: decorate active record and his associations with anything
|
96
|
+
test_files: []
|