liquid4-rails5 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +2 -0
- data/.gitignore +24 -0
- data/.rspec +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +22 -0
- data/CHANGELOG.md +36 -0
- data/Gemfile +12 -0
- data/Guardfile +33 -0
- data/LICENSE.txt +22 -0
- data/README.md +156 -0
- data/Rakefile +37 -0
- data/gemfiles/rails_32.gemfile +11 -0
- data/gemfiles/rails_40.gemfile +11 -0
- data/gemfiles/rails_41.gemfile +11 -0
- data/gemfiles/rails_42.gemfile +11 -0
- data/lib/liquid-rails/drops/collection_drop.rb +91 -0
- data/lib/liquid-rails/drops/drop.rb +130 -0
- data/lib/liquid-rails/drops/droppable.rb +22 -0
- data/lib/liquid-rails/file_system.rb +16 -0
- data/lib/liquid-rails/filters/asset_tag_filter.rb +23 -0
- data/lib/liquid-rails/filters/asset_url_filter.rb +35 -0
- data/lib/liquid-rails/filters/date_filter.rb +17 -0
- data/lib/liquid-rails/filters/google_static_map_url_filter.rb +30 -0
- data/lib/liquid-rails/filters/misc_filter.rb +29 -0
- data/lib/liquid-rails/filters/number_filter.rb +22 -0
- data/lib/liquid-rails/filters/paginate_filter.rb +59 -0
- data/lib/liquid-rails/filters/sanitize_filter.rb +17 -0
- data/lib/liquid-rails/filters/text_filter.rb +44 -0
- data/lib/liquid-rails/filters/translate_filter.rb +14 -0
- data/lib/liquid-rails/filters/url_filter.rb +24 -0
- data/lib/liquid-rails/liquid_monkey_patch.rb +106 -0
- data/lib/liquid-rails/matchers.rb +5 -0
- data/lib/liquid-rails/railtie.rb +26 -0
- data/lib/liquid-rails/rspec/drop_example_group.rb +38 -0
- data/lib/liquid-rails/rspec/drop_matchers.rb +165 -0
- data/lib/liquid-rails/rspec/filter_example_group.rb +24 -0
- data/lib/liquid-rails/rspec/tag_example_group.rb +24 -0
- data/lib/liquid-rails/rspec/view_controller_context.rb +45 -0
- data/lib/liquid-rails/tags/content_for_tag.rb +77 -0
- data/lib/liquid-rails/tags/csrf_meta_tags.rb +11 -0
- data/lib/liquid-rails/tags/google_analytics_tag.rb +40 -0
- data/lib/liquid-rails/tags/javascript_tag.rb +22 -0
- data/lib/liquid-rails/tags/paginate_tag.rb +118 -0
- data/lib/liquid-rails/template_handler.rb +44 -0
- data/lib/liquid-rails/version.rb +5 -0
- data/lib/liquid-rails.rb +24 -0
- data/liquid-rails.gemspec +26 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/images/.keep +0 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/controllers/foospace/bar_controller.rb +5 -0
- data/spec/dummy/app/controllers/home_controller.rb +28 -0
- data/spec/dummy/app/controllers/pages_controller.rb +16 -0
- data/spec/dummy/app/helpers/application_helper.rb +5 -0
- data/spec/dummy/app/mailers/.keep +0 -0
- data/spec/dummy/app/models/.keep +0 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/views/foospace/bar/_partial.liquid +1 -0
- data/spec/dummy/app/views/foospace/bar/index_partial.liquid +3 -0
- data/spec/dummy/app/views/home/_html_liquid_partial.liquid +1 -0
- data/spec/dummy/app/views/home/_partial.liquid +1 -0
- data/spec/dummy/app/views/home/erb_with_html_liquid_partial.erb +1 -0
- data/spec/dummy/app/views/home/index.liquid +1 -0
- data/spec/dummy/app/views/home/index_partial.liquid +4 -0
- data/spec/dummy/app/views/home/index_partial_with_full_path.liquid +4 -0
- data/spec/dummy/app/views/home/index_with_filter.liquid +2 -0
- data/spec/dummy/app/views/home/index_with_layout.liquid +1 -0
- data/spec/dummy/app/views/layouts/application.liquid +2 -0
- data/spec/dummy/app/views/pages/index_without_filter.liquid +2 -0
- data/spec/dummy/app/views/shared/_partial.liquid +1 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/config/application.rb +25 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +29 -0
- data/spec/dummy/config/environments/production.rb +80 -0
- data/spec/dummy/config/environments/test.rb +36 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +17 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/locales/km.yml +5 -0
- data/spec/dummy/config/routes.rb +14 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/lib/assets/.keep +0 -0
- data/spec/dummy/log/.keep +0 -0
- data/spec/dummy/public/404.html +58 -0
- data/spec/dummy/public/422.html +58 -0
- data/spec/dummy/public/500.html +57 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/fixtures/poro.rb +58 -0
- data/spec/lib/liquid-rails/drops/drop_spec.rb +119 -0
- data/spec/lib/liquid-rails/filters/asset_tag_filter_spec.rb +22 -0
- data/spec/lib/liquid-rails/filters/asset_url_filter_spec.rb +34 -0
- data/spec/lib/liquid-rails/filters/google_static_map_url_filter_spec.rb +23 -0
- data/spec/lib/liquid-rails/filters/misc_filter_spec.rb +26 -0
- data/spec/lib/liquid-rails/filters/translate_filter_spec.rb +29 -0
- data/spec/lib/liquid-rails/railtie_spec.rb +9 -0
- data/spec/lib/liquid-rails/rspec/drop_matchers_spec.rb +34 -0
- data/spec/lib/liquid-rails/tags/content_for_tag_spec.rb +33 -0
- data/spec/lib/liquid-rails/tags/csrf_meta_tag_spec.rb +25 -0
- data/spec/lib/liquid-rails/tags/google_analtyics_tag_spec.rb +28 -0
- data/spec/lib/liquid-rails/tags/javascript_tag_spec.rb +14 -0
- data/spec/lib/liquid-rails/tags/paginate_tag_spec.rb +160 -0
- data/spec/lib/liquid-rails/template_handler_spec.rb +67 -0
- data/spec/spec_helper.rb +42 -0
- data/spec/support/delegate_matcher.rb +60 -0
- metadata +274 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: '04811eb8e8a59f3c13d332dfe1c4dc160f886afd'
|
4
|
+
data.tar.gz: 486cd19af25af23e3a5af0947e34b947db765722
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7fd3e1e483bf787f467f0b0d3cd6fe3b2ea15a8516b187556df6317a70c4a3a2e951504fa23b1e87ce2e71b1110b6e51fa13774fe5df39ce1512db53663571f0
|
7
|
+
data.tar.gz: 47eaccbc231b7f557eb29ff114d81597de81c3b51f9a11114df2a2894979b4e9a6b3601099cebde346cbe83e72c195682f8868732189c3cc7cb8528b7f5b6ff5
|
data/.coveralls.yml
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
spec/dummy/log/*
|
24
|
+
gemfiles/*.lock
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.3.3
|
data/.travis.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
language: ruby
|
2
|
+
script: "bundle exec rake spec"
|
3
|
+
rvm:
|
4
|
+
- 2.0.0
|
5
|
+
- 2.1.5
|
6
|
+
- 2.2.0
|
7
|
+
env:
|
8
|
+
- CODECLIMATE_REPO_TOKEN=891d362268d07d6ff0f5534f92252b6195f6be8795054d3627643eb6314a8c9e
|
9
|
+
gemfile:
|
10
|
+
- gemfiles/rails_32.gemfile
|
11
|
+
- gemfiles/rails_40.gemfile
|
12
|
+
- gemfiles/rails_41.gemfile
|
13
|
+
- gemfiles/rails_42.gemfile
|
14
|
+
notifications:
|
15
|
+
email: false
|
16
|
+
matrix:
|
17
|
+
exclude:
|
18
|
+
- rvm: 2.2.0
|
19
|
+
gemfile: gemfiles/rails_32.gemfile
|
20
|
+
addons:
|
21
|
+
code_climate:
|
22
|
+
repo_token: 891d362268d07d6ff0f5534f92252b6195f6be8795054d3627643eb6314a8c9e
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Overview
|
2
|
+
|
3
|
+
## 0.1.2
|
4
|
+
|
5
|
+
### New Features
|
6
|
+
|
7
|
+
* Use google analytics universal (Chamnap Chhorn)
|
8
|
+
|
9
|
+
* Render liquid template as html_safe by default (Dan Kubb)
|
10
|
+
|
11
|
+
## 0.1.1
|
12
|
+
|
13
|
+
### New Features
|
14
|
+
|
15
|
+
* Add `bootstrap_pagination` filter. (Radin Reth)
|
16
|
+
|
17
|
+
* Allow `translate` filter with interpolation. (Tomasz Stachewicz, Chamnap Chhorn)
|
18
|
+
|
19
|
+
* Support `rails` 4.2 and `ruby` 2.2. (Chamnap Chhorn)
|
20
|
+
|
21
|
+
* Support `scope` on collection drop. (Radin Reth)
|
22
|
+
|
23
|
+
|
24
|
+
### Resolved Issues
|
25
|
+
|
26
|
+
* Add `rel="prev"` and `rel="next"` to `default_pagination` filter. (Radin Reth)
|
27
|
+
|
28
|
+
* Fix `content_for` and `yield` tag on `rails` 3.2. (Chamnap Chhorn)
|
29
|
+
|
30
|
+
* \#4 Makes partial template work in namespaced controller. (Tomasz Stachewicz)
|
31
|
+
|
32
|
+
* `truncate` filter now forwards to the standard filters. (Radin Reth)
|
33
|
+
|
34
|
+
## 0.1.0
|
35
|
+
|
36
|
+
* Initial Release
|
data/Gemfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in liquid-rails.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
gem 'rails', '~> 4.0.13'
|
7
|
+
gem 'rspec-rails', '~> 3.2.0'
|
8
|
+
gem 'guard-rspec', '~> 4.5.0'
|
9
|
+
gem 'capybara', '~> 2.4.1'
|
10
|
+
gem 'pry-rails'
|
11
|
+
gem 'coveralls'
|
12
|
+
gem 'simplecov'
|
data/Guardfile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
5
|
+
# rspec may be run, below are examples of the most common uses.
|
6
|
+
# * bundler: 'bundle exec rspec'
|
7
|
+
# * bundler binstubs: 'bin/rspec'
|
8
|
+
# * spring: 'bin/rsspec' (This will use spring if running and you have
|
9
|
+
# installed the spring binstubs per the docs)
|
10
|
+
# * zeus: 'zeus rspec' (requires the server to be started separetly)
|
11
|
+
# * 'just' rspec: 'rspec'
|
12
|
+
guard :rspec, cmd: 'bundle exec rspec' do
|
13
|
+
watch(%r{^spec/.+_spec\.rb$})
|
14
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
15
|
+
watch('spec/spec_helper.rb') { "spec" }
|
16
|
+
|
17
|
+
# Rails example
|
18
|
+
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
19
|
+
watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
20
|
+
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
|
21
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
22
|
+
watch('config/routes.rb') { "spec/routing" }
|
23
|
+
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
24
|
+
watch('spec/rails_helper.rb') { "spec" }
|
25
|
+
|
26
|
+
# Capybara features specs
|
27
|
+
watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
|
28
|
+
|
29
|
+
# Turnip features and steps
|
30
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
31
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
|
32
|
+
end
|
33
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Chamnap Chhorn
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
# Liquid4-Rails5
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/vfonic/liquid4-rails5.svg?branch=master)](https://travis-ci.org/vfonic/liquid4-rails5)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/vfonic/liquid4-rails5/badges/gpa.svg)](https://codeclimate.com/github/vfonic/liquid4-rails5)
|
5
|
+
[![Test Coverage](https://codeclimate.com/github/vfonic/liquid4-rails5/badges/coverage.svg)](https://codeclimate.com/github/vfonic/liquid4-rails5/coverage)
|
6
|
+
[![Gem Version](https://badge.fury.io/rb/liquid4-rails5.svg)](http://badge.fury.io/rb/liquid4-rails5)
|
7
|
+
[![security](https://hakiri.io/github/vfonic/liquid4-rails5/master.svg)](https://hakiri.io/github/vfonic/liquid4-rails5/master)
|
8
|
+
[![git.legal](https://git.legal/projects/3620/badge.svg?key=5c097d05a251758abac3 "Number of libraries approved")](https://git.legal/projects/3620)
|
9
|
+
|
10
|
+
It allows you to render `.liquid` templates with layout and partial support. It also provides filters, tags, drops class to be used inside your liquid template.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
gem 'liquid-rails'
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install liquid-rails
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
In order to render with layout, in your layout file `app/views/layouts/application.liquid`, put this:
|
29
|
+
|
30
|
+
```html
|
31
|
+
{{ content_for_layout }}
|
32
|
+
```
|
33
|
+
|
34
|
+
```html
|
35
|
+
# It will render app/views/home/_partial.liquid when the current controller is `HomeController`.
|
36
|
+
{% include 'partial' %}
|
37
|
+
|
38
|
+
# It will render app/views/shared/_partial.liquid.
|
39
|
+
{% include 'shared/partial' %}
|
40
|
+
```
|
41
|
+
|
42
|
+
### Template Rendering
|
43
|
+
|
44
|
+
By default, **Liquid-Rails** makes all instance variables from controller available to liquid template. To limit only some instance variables, do this inside your controller:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
def liquid_assigns
|
48
|
+
{ 'listing' => current_listing, 'content_for_header' => content_for_header, 'current_account' => current_account }
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
By default, **Liquid-Rails** makes all your helper methods inside your rails app available to liquid template. To limit only some helpers, do this inside your controller:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
def liquid_filters
|
56
|
+
[]
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
You can render liquid templates from other template engines, eg. `erb`, `haml`, ...
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
= render 'shared/partial.liquid'
|
64
|
+
```
|
65
|
+
|
66
|
+
### Filter
|
67
|
+
|
68
|
+
> Filters are simple methods that modify the output of numbers, strings, variables and objects. They are placed within an output tag `{{` `}}` and are separated with a pipe character `|`.
|
69
|
+
|
70
|
+
Currently, **Liquid-Rails** adds only the followings:
|
71
|
+
|
72
|
+
1. [AssetTagFilter](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/filters/asset_tag_filter.rb)
|
73
|
+
2. [AssetUrlFilter](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/filters/asset_url_filter.rb)
|
74
|
+
3. [DateFilter](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/filters/date_filter.rb)
|
75
|
+
4. [NumberFilter](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/filters/number_filter.rb)
|
76
|
+
5. [SanitizeFilter](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/filters/sanitize_filter.rb)
|
77
|
+
6. [TextFilter](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/filters/text_filter.rb)
|
78
|
+
7. [TranslateFilter](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/filters/translate_filter.rb)
|
79
|
+
8. [UrlFilter](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/filters/url_filter.rb)
|
80
|
+
9. [MiscFilter](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/filters/misc_filter.rb)
|
81
|
+
|
82
|
+
### Tag
|
83
|
+
|
84
|
+
> Liquid tags are the programming logic that tells templates what to do. Tags are wrapped in: `{%` `%}`.
|
85
|
+
|
86
|
+
Currently, **Liquid-Rails** adds only the followings:
|
87
|
+
|
88
|
+
1. [csrf_meta_tags](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/tags/csrf_meta_tags.rb)
|
89
|
+
2. [google_analytics_tag](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/tags/google_analytics_tag.rb)
|
90
|
+
3. [javascript_tag](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/tags/javascript_tag.rb)
|
91
|
+
4. [paginate](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/tags/paginate_tag.rb)
|
92
|
+
4. [content_for](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/tags/content_for.rb)
|
93
|
+
|
94
|
+
### Drop Class
|
95
|
+
|
96
|
+
> Drops let you provide the user with custom functionality. They are very much like a standard Ruby class, but have all unused and potentially dangerous methods removed. From the user's perspective a drop acts very much like a Hash, though methods are accessed with dot-notation as well as element selection. A drop method cannot be invoked with arguments. Drops are called just-in-time, thus allowing you to lazily load objects.
|
97
|
+
|
98
|
+
Given two models, a Post(title: string, body: text) and a Comment(name:string, body:text, post_id:integer), you will have two drops:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
class PostDrop < Liquid::Rails::Drop
|
102
|
+
attributes :id, :title, :body
|
103
|
+
|
104
|
+
has_many :comments
|
105
|
+
end
|
106
|
+
```
|
107
|
+
|
108
|
+
and
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
class CommentDrop < Liquid::Rails::Drop
|
112
|
+
attributes :id, :name, :body
|
113
|
+
|
114
|
+
belongs_to :post
|
115
|
+
end
|
116
|
+
```
|
117
|
+
|
118
|
+
Check out more [examples](https://github.com/yoolk/liquid-rails/blob/master/spec/fixtures/poro.rb).
|
119
|
+
|
120
|
+
It works for any ORMs. The PORO should include `Liquid::Rails::Droppable`. That's all you need to do to have your POROs supported.
|
121
|
+
|
122
|
+
### RSpec
|
123
|
+
|
124
|
+
In spec_helper.rb, you'll need to require the matchers:
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
require 'liquid-rails/matchers'
|
128
|
+
```
|
129
|
+
|
130
|
+
Example:
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
describe PostDrop do
|
134
|
+
it { should have_attribute(:id) }
|
135
|
+
it { should have_attribute(:title) }
|
136
|
+
it { should have_attribute(:body) }
|
137
|
+
it { should have_many(:comments) }
|
138
|
+
end
|
139
|
+
```
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
describe CommentDrop do
|
143
|
+
it { should have_attribute(:id) }
|
144
|
+
it { should have_attribute(:name) }
|
145
|
+
it { should have_attribute(:body) }
|
146
|
+
it { should belongs_to(:post) }
|
147
|
+
end
|
148
|
+
```
|
149
|
+
|
150
|
+
## Contributors
|
151
|
+
|
152
|
+
* [Radin Reth](https://github.com/radin-reth/)
|
153
|
+
|
154
|
+
## Authors
|
155
|
+
|
156
|
+
* [Chamnap Chhorn](https://github.com/chamnap)
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# temp fix for NoMethodError: undefined method `last_comment'
|
2
|
+
# remove when fixed in Rake 11.x
|
3
|
+
module TempFixForRakeLastComment
|
4
|
+
def last_comment
|
5
|
+
last_description
|
6
|
+
end
|
7
|
+
end
|
8
|
+
Rake::Application.send :include, TempFixForRakeLastComment
|
9
|
+
### end of temfix
|
10
|
+
|
11
|
+
require 'bundler/gem_tasks'
|
12
|
+
require 'rspec/core'
|
13
|
+
require 'rspec/core/rake_task'
|
14
|
+
|
15
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
16
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
17
|
+
end
|
18
|
+
|
19
|
+
task default: 'spec:all'
|
20
|
+
|
21
|
+
namespace :spec do
|
22
|
+
%w(rails_42 rails_41 rails_40 rails_32).each do |gemfile|
|
23
|
+
desc "Run Tests against #{gemfile}"
|
24
|
+
task gemfile do
|
25
|
+
sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle --quiet"
|
26
|
+
sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle exec rake spec"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
desc "Run Tests against rails versions"
|
31
|
+
task :all do
|
32
|
+
%w(rails_42 rails_41 rails_40 rails_32).each do |gemfile|
|
33
|
+
sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle --quiet"
|
34
|
+
sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle exec rake spec"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
module Liquid
|
2
|
+
module Rails
|
3
|
+
class CollectionDrop < ::Liquid::Drop
|
4
|
+
|
5
|
+
class << self
|
6
|
+
attr_accessor :_scopes
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.inherited(base)
|
10
|
+
base._scopes = []
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.scope(*scope_names)
|
14
|
+
@_scopes.concat scope_names
|
15
|
+
|
16
|
+
scope_names.each do |scope_name|
|
17
|
+
define_method(scope_name) do
|
18
|
+
value = instance_variable_get("@_#{scope_name}")
|
19
|
+
return value if value
|
20
|
+
|
21
|
+
raise ArgumentError, "#{objects.class.name} doesn't define scope: #{scope_name}" unless objects.respond_to?(scope_name)
|
22
|
+
instance_variable_set("@_#{scope_name}", self.class.new(objects.send(scope_name)))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
array_methods = Array.instance_methods - Object.instance_methods
|
28
|
+
delegate *array_methods, to: :dropped_collection
|
29
|
+
delegate :total_count, :total_pages, to: :objects
|
30
|
+
|
31
|
+
def initialize(objects, options={})
|
32
|
+
options.assert_valid_keys(:with, :scope)
|
33
|
+
|
34
|
+
@objects = options[:scope].nil? ? objects : objects.send(options[:scope])
|
35
|
+
@drop_class_name = options[:with]
|
36
|
+
end
|
37
|
+
|
38
|
+
def page(number)
|
39
|
+
self.class.new(objects.page(number))
|
40
|
+
end
|
41
|
+
|
42
|
+
def per(number)
|
43
|
+
self.class.new(objects.per(number))
|
44
|
+
end
|
45
|
+
|
46
|
+
def dropped_collection
|
47
|
+
@dropped_collection ||= @objects.map { |item| drop_item(item) }
|
48
|
+
end
|
49
|
+
|
50
|
+
def kind_of?(klass)
|
51
|
+
dropped_collection.kind_of?(klass) || super
|
52
|
+
end
|
53
|
+
alias_method :is_a?, :kind_of?
|
54
|
+
|
55
|
+
## :[] is invoked by parser before the actual. However, this method has the same name as array method.
|
56
|
+
## Override this, so it will work for both cases.
|
57
|
+
## {{ post_drop.comments[0] }}
|
58
|
+
## {{ post_drop.<other_methods> }}
|
59
|
+
def [](method)
|
60
|
+
if method.is_a?(Integer)
|
61
|
+
dropped_collection.at(method)
|
62
|
+
else
|
63
|
+
public_send(method)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
## Need to override this. I don't understand too, otherwise it will return an array of drop objects.
|
68
|
+
## Need to return self so that we can do chaining.
|
69
|
+
def to_liquid
|
70
|
+
self
|
71
|
+
end
|
72
|
+
|
73
|
+
def inspect
|
74
|
+
"#<#{self.class.name} of #{drop_class} for #{objects.inspect}>"
|
75
|
+
end
|
76
|
+
|
77
|
+
protected
|
78
|
+
|
79
|
+
attr_reader :objects
|
80
|
+
|
81
|
+
def drop_class
|
82
|
+
@drop_class ||= @drop_class_name.is_a?(String) ? @drop_class_name.safe_constantize : @drop_class_name
|
83
|
+
end
|
84
|
+
|
85
|
+
def drop_item(item)
|
86
|
+
liquid_drop_class = drop_class || item.drop_class
|
87
|
+
liquid_drop_class.new(item)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
module Liquid
|
2
|
+
module Rails
|
3
|
+
class Drop < ::Liquid::Drop
|
4
|
+
|
5
|
+
class << self
|
6
|
+
attr_accessor :_attributes
|
7
|
+
attr_accessor :_associations
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.inherited(base)
|
11
|
+
base._attributes = []
|
12
|
+
base._associations = {}
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.attributes(*attrs)
|
16
|
+
@_attributes.concat attrs
|
17
|
+
|
18
|
+
attrs.each do |attr|
|
19
|
+
next if method_defined?(attr)
|
20
|
+
|
21
|
+
define_method(attr) do
|
22
|
+
object.send(attr)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# By default, `Product` maps to `ProductDrop`.
|
28
|
+
# Array of products maps to `Liquid::Rails::CollectionDrop`.
|
29
|
+
def self.drop_class_for(resource)
|
30
|
+
if resource.respond_to?(:to_ary)
|
31
|
+
Liquid::Rails::CollectionDrop
|
32
|
+
else
|
33
|
+
if self == Liquid::Rails::Drop
|
34
|
+
resource.drop_class
|
35
|
+
else
|
36
|
+
self
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Create a drop instance when it cannot be inferred.
|
42
|
+
def self.dropify(resource, options={})
|
43
|
+
drop_class = if options[:class_name]
|
44
|
+
options[:class_name].constantize
|
45
|
+
else
|
46
|
+
drop_class_for(resource)
|
47
|
+
end
|
48
|
+
|
49
|
+
drop_class.new(resource, options.except(:class_name))
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.belongs_to(*attrs)
|
53
|
+
associate(:belongs_to, attrs)
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.has_many(*attrs)
|
57
|
+
associate(:has_many, attrs)
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.has_one(*attrs)
|
61
|
+
associate(:has_one, attrs)
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.associate(type, attrs)
|
65
|
+
options = attrs.extract_options!
|
66
|
+
self._associations = _associations.dup
|
67
|
+
|
68
|
+
attrs.each do |attr|
|
69
|
+
next if method_defined?(attr)
|
70
|
+
|
71
|
+
define_method attr do
|
72
|
+
value = instance_variable_get("@_#{attr}")
|
73
|
+
return value if value
|
74
|
+
|
75
|
+
association = object.send(attr)
|
76
|
+
return nil if association.nil?
|
77
|
+
|
78
|
+
drop_instance = Liquid::Rails::Drop.dropify(association, options)
|
79
|
+
instance_variable_set("@_#{attr}", drop_instance)
|
80
|
+
end
|
81
|
+
|
82
|
+
self._associations[attr] = { type: type, options: options }
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# Wraps an object in a new instance of the drop.
|
87
|
+
def initialize(object, options={})
|
88
|
+
@object = object
|
89
|
+
end
|
90
|
+
|
91
|
+
def attributes
|
92
|
+
@attributes ||= self.class._attributes.dup.each_with_object({}) do |name, hash|
|
93
|
+
hash[name.to_s] = send(name)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def as_json(options={})
|
98
|
+
attributes.as_json(options)
|
99
|
+
end
|
100
|
+
|
101
|
+
def to_json(options={})
|
102
|
+
as_json.to_json(options)
|
103
|
+
end
|
104
|
+
|
105
|
+
def before_method(method)
|
106
|
+
attributes[method.to_s]
|
107
|
+
end
|
108
|
+
|
109
|
+
def liquid_method_missing(method)
|
110
|
+
return nil unless @context && @context.strict_variables
|
111
|
+
raise ::Liquid::UndefinedDropMethod, <<~HEREDOC
|
112
|
+
undefined method #{method} for #{self.class}
|
113
|
+
Did you forget to add it to `attributes`?
|
114
|
+
HEREDOC
|
115
|
+
end
|
116
|
+
|
117
|
+
def inspect
|
118
|
+
"#<#{self.class.name} @object: #{object.inspect} @attributes: #{attributes.inspect}>"
|
119
|
+
end
|
120
|
+
|
121
|
+
def ==(other)
|
122
|
+
other.present? && other.object == @object
|
123
|
+
end
|
124
|
+
|
125
|
+
protected
|
126
|
+
|
127
|
+
attr_reader :object
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Liquid
|
2
|
+
module Rails
|
3
|
+
module Droppable
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
def to_liquid
|
7
|
+
drop_class.new(self)
|
8
|
+
end
|
9
|
+
alias_method :dropify, :to_liquid
|
10
|
+
|
11
|
+
def drop_class
|
12
|
+
self.class.drop_class
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
def drop_class
|
17
|
+
"#{self.name}Drop".constantize
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'liquid/file_system'
|
2
|
+
|
3
|
+
module Liquid
|
4
|
+
module Rails
|
5
|
+
class FileSystem < ::Liquid::LocalFileSystem
|
6
|
+
def read_template_file(template_path, context)
|
7
|
+
unless template_path.include?('/')
|
8
|
+
controller_path = context.registers[:controller].controller_path
|
9
|
+
template_path = "#{controller_path}/#{template_path}"
|
10
|
+
end
|
11
|
+
|
12
|
+
super
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|