view_delegates 0.1.0
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/MIT-LICENSE +20 -0
- data/README.md +74 -0
- data/Rakefile +32 -0
- data/app/assets/javascripts/view_delegate.js +2 -0
- data/app/assets/stylesheets/view_delegate.css +4 -0
- data/app/controllers/view_delegate_controller.rb +15 -0
- data/app/helpers/view_delegate_helper.rb +2 -0
- data/lib/view_delegates/engine.rb +4 -0
- data/lib/view_delegates/poros/cache.rb +38 -0
- data/lib/view_delegates/poros/view_delegate.rb +105 -0
- data/lib/view_delegates/version.rb +3 -0
- data/lib/view_delegates.rb +6 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 560994ea7de1356f1e32811a09cc2d09d2f7c1680f84b4b0522564f4cb7a4ce1
|
4
|
+
data.tar.gz: 3e73b82aca43a8f57421bb45e112a085ac75b66cc37608f8861680260f2d8fbf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 823cf54f2f78a396422e51c1a08141892fefcd9827b307f4b8538ea7d4efa0c9fdd3886f2a1fd8ba8d0ad42ca98f8d28bcdaa1a9e1a305032836acf88192face
|
7
|
+
data.tar.gz: a3f418e905c5377ca304863e1c1d80718500e49915253563e80b625ca3b7ea9c9ca257ff67c873b307c745e3eaaba63d077db65849e041a5f6ee4d917c6bc0a4
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2018
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
<a href="https://codeclimate.com/github/coreSegmentFault/view_delegates/maintainability"><img src="https://api.codeclimate.com/v1/badges/a74e2a9f9198b29683a2/maintainability" /></a>
|
2
|
+
# ViewDelegates
|
3
|
+
ViewDelegates makes easy to write reusable view components with decoupled functionality from
|
4
|
+
the active record models
|
5
|
+
|
6
|
+
Create a ruby class implementing this gem base class
|
7
|
+
ViewDelegates::ViewDelegate. I recommend to place the view delegates
|
8
|
+
under a folder on your applications app named '/view_delegates'. Then
|
9
|
+
just add your logic.
|
10
|
+
```ruby
|
11
|
+
module Admin
|
12
|
+
class AdminTestDelegate < ViewDelegates::ViewDelegate
|
13
|
+
view_local :test_method
|
14
|
+
property :my_property
|
15
|
+
model :dummy, properties: [:a]
|
16
|
+
cache true
|
17
|
+
def test_method
|
18
|
+
'test_method'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
```
|
23
|
+
- view_local executes a method of the class and adds it to the render as a local parameter.
|
24
|
+
- property creates an accessor to add extra variables to the view delegate
|
25
|
+
- model also creates an accessor but permits to reject other properties not wanted to have in your view. pass to to the properties array any properties you will need in your view
|
26
|
+
and the rest will be discarted
|
27
|
+
- cache is an optional parameter, pass a size: parameter to change the default size of the cache pool. Default: 50
|
28
|
+
|
29
|
+
## Render views
|
30
|
+
Create an instance from the delegate you want to render
|
31
|
+
|
32
|
+
@delegate = Admin::AdminTestDelegate.new(dummy: @dummy, my_property: 'My property test')
|
33
|
+
|
34
|
+
And call render from that instance passing as a symbol the view you want to render
|
35
|
+
@delegate.render(:index)
|
36
|
+
If you want to use any extra params on one view
|
37
|
+
@delegate.render(:index, local_params: {optional_param: 'coreSegmentFault'
|
38
|
+
|
39
|
+
To know where your class will look for your view, open your console and call .view_path
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
2.5.0 :004 > Admin::AdminTestDelegate.view_path
|
43
|
+
=> "admin/admin_test"
|
44
|
+
|
45
|
+
```
|
46
|
+
|
47
|
+
## Usage
|
48
|
+
How to use my plugin.
|
49
|
+
|
50
|
+
## Installation
|
51
|
+
Add this line to your application's Gemfile:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
gem 'view_delegates'
|
55
|
+
```
|
56
|
+
|
57
|
+
And then execute:
|
58
|
+
```bash
|
59
|
+
$ bundle
|
60
|
+
```
|
61
|
+
|
62
|
+
Or install it yourself as:
|
63
|
+
```bash
|
64
|
+
$ gem install view_delegates
|
65
|
+
```
|
66
|
+
|
67
|
+
## Contributing
|
68
|
+
Any contribution is accepted gladly. We only require two things:
|
69
|
+
- Add test cases for your added functionaly
|
70
|
+
- Add documentation
|
71
|
+
- Don't change any existing functionality for retro-compatibility
|
72
|
+
|
73
|
+
## License
|
74
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'ViewDelegates'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path('test/dummy/Rakefile', __dir__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
load 'rails/tasks/statistics.rake'
|
21
|
+
|
22
|
+
require 'bundler/gem_tasks'
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'test'
|
28
|
+
t.pattern = 'test/**/*_test.rb'
|
29
|
+
t.verbose = false
|
30
|
+
end
|
31
|
+
|
32
|
+
task default: :test
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class ViewDelegateController < ActionController::Base
|
2
|
+
MODULES = [
|
3
|
+
ActionController::ConditionalGet,
|
4
|
+
ActionController::EtagWithTemplateDigest,
|
5
|
+
ActionController::EtagWithFlash,
|
6
|
+
ActionController::Caching,
|
7
|
+
ActionController::ImplicitRender,
|
8
|
+
ActionController::StrongParameters,
|
9
|
+
ActionController::ParameterEncoding,
|
10
|
+
ActionController::Cookies,
|
11
|
+
ActionController::Flash,
|
12
|
+
ActionController::FormBuilder
|
13
|
+
].freeze
|
14
|
+
without_modules MODULES
|
15
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module ViewDelegates
|
2
|
+
class Cache
|
3
|
+
CacheEntry = Struct.new(:key, :value)
|
4
|
+
attr_accessor :entries
|
5
|
+
def initialize(max_size: 10)
|
6
|
+
@entries = []
|
7
|
+
@max_size = max_size
|
8
|
+
end
|
9
|
+
|
10
|
+
def add(key:, value:)
|
11
|
+
@entries << CacheEntry.new(key, value)
|
12
|
+
if @entries.length > @max_size
|
13
|
+
@entries.delete_at 0
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def get(key)
|
18
|
+
result = nil
|
19
|
+
index = @entries.index { |e| e.key == key }
|
20
|
+
if index
|
21
|
+
result = @entries[index].value
|
22
|
+
update_element index
|
23
|
+
end
|
24
|
+
result
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def update_element(index)
|
30
|
+
before = index - 1
|
31
|
+
start = []
|
32
|
+
start = @entries[0..before] unless before.negative?
|
33
|
+
@entries = start, @entries[index..(index + 1)].reverse,
|
34
|
+
@entries[(index + 2)..-1]
|
35
|
+
@entries = @entries.flatten.uniq
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
module ViewDelegates
|
2
|
+
# Base class for delegates
|
3
|
+
class ViewDelegate
|
4
|
+
# This property will contain all the methods the view delegate
|
5
|
+
# will execute to add as view locals
|
6
|
+
@@view_locals = []
|
7
|
+
# All models this view delegate will contain
|
8
|
+
@@ar_models = []
|
9
|
+
# Properties
|
10
|
+
@@properties = []
|
11
|
+
# View delegate cache system
|
12
|
+
# @return [ViewDelegates::Cache]
|
13
|
+
def delegate_cache
|
14
|
+
@@delegate_cache
|
15
|
+
end
|
16
|
+
# Initialize method
|
17
|
+
# @param [Hash] view_data hash containing all delegate properties
|
18
|
+
def initialize(view_data = {})
|
19
|
+
@@ar_models.each do |t|
|
20
|
+
send("#{t}=", view_data[t]) if view_data[t]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Renders as a string the view passed as params
|
25
|
+
# @param [Symbol] view
|
26
|
+
def render(view, local_params: {})
|
27
|
+
locals = {}.merge(local_params)
|
28
|
+
@@view_locals.each do |method|
|
29
|
+
locals[method] = send(method)
|
30
|
+
end
|
31
|
+
ar_models = {}
|
32
|
+
@@ar_models.each do |ar_model|
|
33
|
+
ar_models[ar_model] = instance_variable_get(:"@#{ar_model}")
|
34
|
+
end
|
35
|
+
@@properties.each do |property|
|
36
|
+
locals[property] = instance_variable_get "@#{property}"
|
37
|
+
end
|
38
|
+
locals = locals.merge(ar_models)
|
39
|
+
ViewDelegateController.render(self.class.view_path + '/' + view.to_s,
|
40
|
+
locals: locals)
|
41
|
+
end
|
42
|
+
class << self
|
43
|
+
def cache(option, size: 50)
|
44
|
+
if option
|
45
|
+
render_method = instance_method :render
|
46
|
+
@@delegate_cache = ViewDelegates::Cache.new(max_size: size)
|
47
|
+
define_method(:render) do |view, local_params: {}|
|
48
|
+
value_key = "#{hash}#{view.to_s}"
|
49
|
+
cache = @@delegate_cache.get value_key
|
50
|
+
if cache.nil?
|
51
|
+
rendered = render_method.bind(self).call(view, local_params)
|
52
|
+
@@delegate_cache.add key: value_key, value: rendered
|
53
|
+
rendered
|
54
|
+
else
|
55
|
+
cache
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
# Gets the path for the delegate views
|
61
|
+
def view_path
|
62
|
+
@view_path ||= to_s.sub(/Delegate/, ''.freeze).underscore
|
63
|
+
end
|
64
|
+
|
65
|
+
# Marks a method as a view local
|
66
|
+
# @param [Symbol] method
|
67
|
+
def view_local(method)
|
68
|
+
@@view_locals << method
|
69
|
+
end
|
70
|
+
|
71
|
+
# View properties
|
72
|
+
# @param [Symbol] method
|
73
|
+
def property(method)
|
74
|
+
@@properties << method
|
75
|
+
attr_accessor method
|
76
|
+
end
|
77
|
+
|
78
|
+
# The models this delegate will use
|
79
|
+
# @param [method] method The variable name this model will use
|
80
|
+
# @param [Array] properties The model properties to extract
|
81
|
+
# from the active record model
|
82
|
+
def model(method, properties: [])
|
83
|
+
attr_accessor method
|
84
|
+
# Add the method name to the array of delegate models
|
85
|
+
@@ar_models << method
|
86
|
+
# Define a setter for the model
|
87
|
+
define_method "#{method}=" do |val|
|
88
|
+
# Create a struct with the model properties
|
89
|
+
model_delegate = if properties.any?
|
90
|
+
Struct.new(*properties)
|
91
|
+
else
|
92
|
+
Struct.new(*val.attributes.keys)
|
93
|
+
end
|
94
|
+
initialize_hash = {}
|
95
|
+
model_delegate.members.each do |k|
|
96
|
+
initialize_hash[k] = val.send k
|
97
|
+
end
|
98
|
+
model_delegate = model_delegate.new(*initialize_hash)
|
99
|
+
# set the struct to instance model
|
100
|
+
instance_variable_set(:"@#{method}", model_delegate)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: view_delegates
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Miguel Aurelio Lamas Murias
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-22 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: 5.1.6
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.1.6
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: byebug
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec-rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '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: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Easy view delegates for ruby on rails
|
98
|
+
email:
|
99
|
+
- miguel.aurelio.murias28@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- MIT-LICENSE
|
105
|
+
- README.md
|
106
|
+
- Rakefile
|
107
|
+
- app/assets/javascripts/view_delegate.js
|
108
|
+
- app/assets/stylesheets/view_delegate.css
|
109
|
+
- app/controllers/view_delegate_controller.rb
|
110
|
+
- app/helpers/view_delegate_helper.rb
|
111
|
+
- lib/view_delegates.rb
|
112
|
+
- lib/view_delegates/engine.rb
|
113
|
+
- lib/view_delegates/poros/cache.rb
|
114
|
+
- lib/view_delegates/poros/view_delegate.rb
|
115
|
+
- lib/view_delegates/version.rb
|
116
|
+
homepage:
|
117
|
+
licenses:
|
118
|
+
- MIT
|
119
|
+
metadata: {}
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubyforge_project:
|
136
|
+
rubygems_version: 2.7.3
|
137
|
+
signing_key:
|
138
|
+
specification_version: 4
|
139
|
+
summary: Easy view delegates for ruby on rails
|
140
|
+
test_files: []
|