liquid-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +2 -0
  3. data/.gitignore +24 -0
  4. data/.rspec +1 -0
  5. data/.ruby-gemset +1 -0
  6. data/.ruby-version +1 -0
  7. data/.travis.yml +17 -0
  8. data/Gemfile +12 -0
  9. data/Guardfile +33 -0
  10. data/LICENSE.txt +22 -0
  11. data/README.md +140 -0
  12. data/Rakefile +27 -0
  13. data/gemfiles/rails_32.gemfile +11 -0
  14. data/gemfiles/rails_40.gemfile +11 -0
  15. data/gemfiles/rails_41.gemfile +11 -0
  16. data/lib/liquid-rails.rb +23 -0
  17. data/lib/liquid-rails/drops/collection_drop.rb +91 -0
  18. data/lib/liquid-rails/drops/drop.rb +114 -0
  19. data/lib/liquid-rails/drops/droppable.rb +22 -0
  20. data/lib/liquid-rails/file_system.rb +14 -0
  21. data/lib/liquid-rails/filters/asset_tag_filter.rb +25 -0
  22. data/lib/liquid-rails/filters/asset_url_filter.rb +37 -0
  23. data/lib/liquid-rails/filters/date_filter.rb +19 -0
  24. data/lib/liquid-rails/filters/google_static_map_url_filter.rb +30 -0
  25. data/lib/liquid-rails/filters/misc_filter.rb +47 -0
  26. data/lib/liquid-rails/filters/number_filter.rb +24 -0
  27. data/lib/liquid-rails/filters/sanitize_filter.rb +19 -0
  28. data/lib/liquid-rails/filters/text_filter.rb +47 -0
  29. data/lib/liquid-rails/filters/translate_filter.rb +13 -0
  30. data/lib/liquid-rails/filters/url_filter.rb +24 -0
  31. data/lib/liquid-rails/matchers.rb +5 -0
  32. data/lib/liquid-rails/railtie.rb +26 -0
  33. data/lib/liquid-rails/rspec/drop_example_group.rb +38 -0
  34. data/lib/liquid-rails/rspec/drop_matchers.rb +159 -0
  35. data/lib/liquid-rails/rspec/filter_example_group.rb +24 -0
  36. data/lib/liquid-rails/rspec/tag_example_group.rb +24 -0
  37. data/lib/liquid-rails/rspec/view_controller_context.rb +44 -0
  38. data/lib/liquid-rails/tags/content_for_tag.rb +68 -0
  39. data/lib/liquid-rails/tags/csrf_meta_tags.rb +11 -0
  40. data/lib/liquid-rails/tags/google_analytics_tag.rb +43 -0
  41. data/lib/liquid-rails/tags/javascript_tag.rb +22 -0
  42. data/lib/liquid-rails/tags/paginate_tag.rb +117 -0
  43. data/lib/liquid-rails/template_handler.rb +44 -0
  44. data/lib/liquid-rails/version.rb +5 -0
  45. data/liquid-rails.gemspec +26 -0
  46. data/spec/dummy/README.rdoc +28 -0
  47. data/spec/dummy/Rakefile +6 -0
  48. data/spec/dummy/app/assets/images/.keep +0 -0
  49. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  50. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  51. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  52. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  53. data/spec/dummy/app/controllers/home_controller.rb +25 -0
  54. data/spec/dummy/app/controllers/pages_controller.rb +16 -0
  55. data/spec/dummy/app/helpers/application_helper.rb +5 -0
  56. data/spec/dummy/app/mailers/.keep +0 -0
  57. data/spec/dummy/app/models/.keep +0 -0
  58. data/spec/dummy/app/models/concerns/.keep +0 -0
  59. data/spec/dummy/app/views/home/_partial.liquid +1 -0
  60. data/spec/dummy/app/views/home/index.liquid +1 -0
  61. data/spec/dummy/app/views/home/index_partial.liquid +4 -0
  62. data/spec/dummy/app/views/home/index_partial_with_full_path.liquid +4 -0
  63. data/spec/dummy/app/views/home/index_with_filter.liquid +2 -0
  64. data/spec/dummy/app/views/home/index_with_layout.liquid +1 -0
  65. data/spec/dummy/app/views/layouts/application.liquid +2 -0
  66. data/spec/dummy/app/views/pages/index_without_filter.liquid +2 -0
  67. data/spec/dummy/app/views/shared/_partial.liquid +1 -0
  68. data/spec/dummy/bin/bundle +3 -0
  69. data/spec/dummy/bin/rails +4 -0
  70. data/spec/dummy/bin/rake +4 -0
  71. data/spec/dummy/config.ru +4 -0
  72. data/spec/dummy/config/application.rb +25 -0
  73. data/spec/dummy/config/boot.rb +5 -0
  74. data/spec/dummy/config/environment.rb +5 -0
  75. data/spec/dummy/config/environments/development.rb +29 -0
  76. data/spec/dummy/config/environments/production.rb +80 -0
  77. data/spec/dummy/config/environments/test.rb +36 -0
  78. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  79. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  80. data/spec/dummy/config/initializers/inflections.rb +16 -0
  81. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  82. data/spec/dummy/config/initializers/secret_token.rb +17 -0
  83. data/spec/dummy/config/initializers/session_store.rb +3 -0
  84. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  85. data/spec/dummy/config/locales/en.yml +4 -0
  86. data/spec/dummy/config/locales/km.yml +4 -0
  87. data/spec/dummy/config/routes.rb +10 -0
  88. data/spec/dummy/lib/assets/.keep +0 -0
  89. data/spec/dummy/public/404.html +58 -0
  90. data/spec/dummy/public/422.html +58 -0
  91. data/spec/dummy/public/500.html +57 -0
  92. data/spec/dummy/public/favicon.ico +0 -0
  93. data/spec/fixtures/poro.rb +58 -0
  94. data/spec/lib/liquid-rails/drops/drop_spec.rb +119 -0
  95. data/spec/lib/liquid-rails/filters/asset_tag_filter_spec.rb +22 -0
  96. data/spec/lib/liquid-rails/filters/asset_url_filter_spec.rb +34 -0
  97. data/spec/lib/liquid-rails/filters/google_static_map_url_filter_spec.rb +23 -0
  98. data/spec/lib/liquid-rails/filters/misc_filter_spec.rb +26 -0
  99. data/spec/lib/liquid-rails/filters/translate_filter_spec.rb +25 -0
  100. data/spec/lib/liquid-rails/railtie_spec.rb +9 -0
  101. data/spec/lib/liquid-rails/rspec/drop_matchers_spec.rb +34 -0
  102. data/spec/lib/liquid-rails/tags/content_for_tag_spec.rb +27 -0
  103. data/spec/lib/liquid-rails/tags/csrf_meta_tag_spec.rb +16 -0
  104. data/spec/lib/liquid-rails/tags/google_analtyics_tag_spec.rb +31 -0
  105. data/spec/lib/liquid-rails/tags/javascript_tag_spec.rb +14 -0
  106. data/spec/lib/liquid-rails/tags/paginate_tag_spec.rb +96 -0
  107. data/spec/lib/liquid-rails/template_handler_spec.rb +53 -0
  108. data/spec/spec_helper.rb +44 -0
  109. data/spec/support/delegate_matcher.rb +60 -0
  110. metadata +261 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5e9723ef54a8b5602a0eece9ecbf5a3eef85e53f
4
+ data.tar.gz: 660456e3598bacf1b5f435bea0b7875d46bd7a54
5
+ SHA512:
6
+ metadata.gz: ff1a8eca8c43f25d3829951b86b55d747abb5d343224568919f7c25be686ca5a11c469de95111c78caede8327c708cb494fdb81af02b10a94cd671e02603bc5b
7
+ data.tar.gz: a596f42ec4296f3863f81dab54123ca1f47fbb6fe2d4c6e81ba401470f7cfd509b3737cebf9ca92c9a6d5be99def38238a4483603a785fd4311387447cf69286
@@ -0,0 +1,2 @@
1
+ service_name: travis-ci
2
+ repo_token: stxQNkczi4haKGRjKZvd7i8PAooQrahCX
@@ -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
@@ -0,0 +1 @@
1
+ liquid-rails
@@ -0,0 +1 @@
1
+ ruby-2.1.3
@@ -0,0 +1,17 @@
1
+ language: ruby
2
+ script: "bundle exec rake spec"
3
+ rvm:
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - 2.1.1
7
+ - 2.1.2
8
+ env:
9
+ - CODECLIMATE_REPO_TOKEN=891d362268d07d6ff0f5534f92252b6195f6be8795054d3627643eb6314a8c9e
10
+ gemfile:
11
+ - gemfiles/rails_40.gemfile
12
+ - gemfiles/rails_41.gemfile
13
+ notifications:
14
+ email: false
15
+ addons:
16
+ code_climate:
17
+ repo_token: 891d362268d07d6ff0f5534f92252b6195f6be8795054d3627643eb6314a8c9e
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.10'
7
+ gem 'rspec-rails', '~> 2.14.2'
8
+ gem 'capybara', '~> 2.4.1'
9
+ gem 'pry-rails'
10
+ gem 'coveralls'
11
+ gem 'simplecov'
12
+ gem 'guard-rspec'
@@ -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
+
@@ -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.
@@ -0,0 +1,140 @@
1
+ [![Build Status](https://travis-ci.org/yoolk/liquid-rails.svg?branch=master)](https://travis-ci.org/yoolk/liquid-rails)[![Coverage Status](https://coveralls.io/repos/yoolk/liquid-rails/badge.png?branch=master)](https://coveralls.io/r/yoolk/liquid-rails?branch=master)
2
+ # Liquid-Rails
3
+
4
+ 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.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'liquid-rails'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install liquid-rails
19
+
20
+ ## Usage
21
+
22
+ In order to render with layout, in your layout file `app/views/layouts/application.liquid`, put this:
23
+
24
+ ```html
25
+ {{ content_for_layout }}
26
+ ```
27
+
28
+ ```html
29
+ # It will render app/views/home/_partial.liquid when the current controller is `HomeController`.
30
+ {% include 'partial' %}
31
+
32
+ # It will render app/views/shared/_partial.liquid.
33
+ {% include 'shared/partial' %}
34
+ ```
35
+
36
+ ### Template Rendering
37
+
38
+ 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:
39
+
40
+ ```ruby
41
+ def liquid_assigns
42
+ { 'listing' => current_listing, 'content_for_header' => content_for_header, 'current_account' => current_account }
43
+ end
44
+ ```
45
+
46
+ 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:
47
+
48
+ ```ruby
49
+ def liquid_filters
50
+ []
51
+ end
52
+ ```
53
+
54
+ ### Filter
55
+
56
+ > 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 `|`.
57
+
58
+ Currently, **Liquid-Rails** adds only the followings:
59
+
60
+ 1. [AssetTagFilter](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/filters/asset_tag_filter.rb)
61
+ 2. [AssetUrlFilter](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/filters/asset_url_filter.rb)
62
+ 3. [DateFilter](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/filters/date_filter.rb)
63
+ 4. [NumberFilter](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/filters/number_filter.rb)
64
+ 5. [SanitizeFilter](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/filters/sanitize_filter.rb)
65
+ 6. [TextFilter](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/filters/text_filter.rb)
66
+ 7. [TranslateFilter](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/filters/translate_filter.rb)
67
+ 8. [UrlFilter](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/filters/url_filter.rb)
68
+ 9. [MiscFilter](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/filters/misc_filter.rb)
69
+
70
+ ### Tag
71
+
72
+ > Liquid tags are the programming logic that tells templates what to do. Tags are wrapped in: `{%` `%}`.
73
+
74
+ Currently, **Liquid-Rails** adds only the followings:
75
+
76
+ 1. [csrf_meta_tags](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/tags/csrf_meta_tags.rb)
77
+ 2. [google_analytics_tag](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/tags/google_analytics_tag.rb)
78
+ 3. [javascript_tag](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/tags/javascript_tag.rb)
79
+ 4. [paginate](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/tags/paginate_tag.rb)
80
+ 4. [content_for](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/tags/content_for.rb)
81
+
82
+ ### Drop Class
83
+
84
+ > 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.
85
+
86
+ Given two models, a Post(title: string, body: text) and a Comment(name:string, body:text, post_id:integer), you will have two drops:
87
+
88
+ ```ruby
89
+ class PostDrop < Liquid::Rails::Drop
90
+ attributes :id, :title, :body
91
+
92
+ has_many :comments
93
+ end
94
+ ```
95
+
96
+ and
97
+
98
+ ```ruby
99
+ class CommentDrop < Liquid::Rails::Drop
100
+ attributes :id, :name, :body
101
+
102
+ belongs_to :post
103
+ end
104
+ ```
105
+
106
+ Check out more [examples](https://github.com/yoolk/liquid-rails/blob/master/spec/fixtures/poro.rb).
107
+
108
+ It works for any ORMs. The PORO should include `Liquid::Rails::Droppable`. That's all you need to do to have your POROs supported.
109
+
110
+ ### RSpec
111
+
112
+ In spec_helper.rb, you'll need to require the matchers:
113
+
114
+ ```ruby
115
+ require 'liquid-rails/matchers'
116
+ ```
117
+
118
+ Example:
119
+
120
+ ```ruby
121
+ describe PostDrop do
122
+ it { should have_attribute(:id) }
123
+ it { should have_attribute(:title) }
124
+ it { should have_attribute(:body) }
125
+ it { should have_many(:comments) }
126
+ end
127
+ ```
128
+
129
+ ```ruby
130
+ describe CommentDrop do
131
+ it { should have_attribute(:id) }
132
+ it { should have_attribute(:name) }
133
+ it { should have_attribute(:body) }
134
+ it { should belongs_to(:post) }
135
+ end
136
+ ```
137
+
138
+ ## Authors
139
+
140
+ * [Chamnap Chhorn](https://github.com/chamnap)
@@ -0,0 +1,27 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = FileList['spec/**/*_spec.rb']
7
+ end
8
+
9
+ task default: 'spec:all'
10
+
11
+ namespace :spec do
12
+ %w(rails_41 rails_40 rails_32).each do |gemfile|
13
+ desc "Run Tests against #{gemfile}"
14
+ task gemfile do
15
+ sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle --quiet"
16
+ sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle exec rake spec"
17
+ end
18
+ end
19
+
20
+ desc "Run Tests against rails versions"
21
+ task :all do
22
+ %w(rails_41 rails_40 rails_32).each do |gemfile|
23
+ sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle --quiet"
24
+ sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle exec rake spec"
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '~> 3.2.19'
4
+ gem 'rspec-rails', '~> 2.14.2'
5
+ gem 'capybara', '~> 2.4.1'
6
+ gem 'pry-rails'
7
+ gem 'coveralls'
8
+ gem 'simplecov'
9
+ gem 'guard-rspec'
10
+
11
+ gemspec path: '../'
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '~> 4.0.10'
4
+ gem 'rspec-rails', '~> 2.14.2'
5
+ gem 'capybara', '~> 2.4.1'
6
+ gem 'pry-rails'
7
+ gem 'coveralls'
8
+ gem 'simplecov'
9
+ gem 'guard-rspec'
10
+
11
+ gemspec path: '../'
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '~> 4.1.6'
4
+ gem 'rspec-rails', '~> 2.14.2'
5
+ gem 'capybara', '~> 2.4.1'
6
+ gem 'pry-rails'
7
+ gem 'coveralls'
8
+ gem 'simplecov'
9
+ gem 'guard-rspec'
10
+
11
+ gemspec path: '../'
@@ -0,0 +1,23 @@
1
+ require 'liquid-rails/version'
2
+ require 'liquid'
3
+ require 'kaminari'
4
+ require 'active_support/concern'
5
+
6
+ module Liquid
7
+ module Rails
8
+ autoload :TemplateHandler, 'liquid-rails/template_handler'
9
+ autoload :FileSystem, 'liquid-rails/file_system'
10
+
11
+ autoload :Drop, 'liquid-rails/drops/drop'
12
+ autoload :CollectionDrop, 'liquid-rails/drops/collection_drop'
13
+
14
+ def self.setup_drop(base)
15
+ base.class_eval do
16
+ include Liquid::Rails::Droppable
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ require 'liquid-rails/railtie' if defined?(Rails)
23
+ Dir[File.dirname(__FILE__) + '/liquid-rails/{filters,tags,drops}/*.rb'].each { |f| require f }
@@ -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)
33
+
34
+ @objects = objects
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