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,22 @@
1
+ # Returns a JavaScript tag with the content inside.
2
+ #
3
+ # Usage:
4
+ #
5
+ # {% javascript_tag %}
6
+ # alert('Hello Liquid Rails!');
7
+ # {% endjavascript_tag %}
8
+
9
+ module Liquid
10
+ module Rails
11
+ class JavascriptTag < ::Liquid::Block
12
+ include ActionView::Helpers::JavaScriptHelper
13
+ include ActionView::Helpers::TagHelper
14
+
15
+ def render(context)
16
+ javascript_tag(super, type: 'text/javascript')
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ Liquid::Template.register_tag('javascript_tag', Liquid::Rails::JavascriptTag)
@@ -0,0 +1,117 @@
1
+ # Paginate a collection
2
+ #
3
+ # Usage:
4
+ #
5
+ # {% paginate listing.photos by 5 %}
6
+ # {% for photo in paginate.collection %}
7
+ # {{ photo.caption }}
8
+ # {% endfor %}
9
+ # {% endpaginate %}
10
+ #
11
+ module Liquid
12
+ module Rails
13
+ class PaginateTag < ::Liquid::Block
14
+ Syntax = /(#{::Liquid::QuotedFragment})\s*(by\s*(\d+))?/
15
+
16
+ def initialize(tag_name, markup, context)
17
+ super
18
+
19
+ if markup =~ Syntax
20
+ @collection_name = $1
21
+ @page_size = if $2
22
+ $3.to_i
23
+ else
24
+ 25
25
+ end
26
+
27
+ @attributes = { 'window_size' => 3 }
28
+ markup.scan(Liquid::TagAttributes) do |key, value|
29
+ @attributes[key] = value
30
+ end
31
+ else
32
+ raise SyntaxError.new("Syntax Error in tag 'paginate' - Valid syntax: paginate [collection] by number")
33
+ end
34
+ end
35
+
36
+ def render(context)
37
+ @context = context
38
+
39
+ context.stack do
40
+ collection = context[@collection_name]
41
+ raise ::Liquid::ArgumentError.new("Cannot paginate array '#{@collection_name}'. Not found.") if collection.nil?
42
+
43
+ if collection.is_a? Array
44
+ paginated_collection = Kaminari.paginate_array(collection.to_a).page(current_page).per(@page_size)
45
+ elsif collection.respond_to?(:page) && collection.respond_to?(:per)
46
+ paginated_collection = collection.page(current_page).per(@page_size)
47
+ end
48
+
49
+ page_count = paginated_collection.total_pages
50
+ pagination = {}
51
+ pagination['collection'] = paginated_collection
52
+ pagination['current_offset'] = (current_page-1) * @page_size
53
+ pagination['page_size'] = @page_size
54
+ pagination['pages'] = page_count
55
+ pagination['items'] = paginated_collection.total_count
56
+ pagination['previous'] = link('&laquo; Previous'.html_safe, current_page-1 ) unless 1 >= current_page
57
+ pagination['next'] = link('Next &raquo;'.html_safe, current_page+1 ) unless page_count <= current_page+1
58
+ pagination['parts'] = []
59
+
60
+ hellip_break = false
61
+ if page_count > 1
62
+ 1.upto(page_count) do |page|
63
+
64
+ if current_page == page
65
+ pagination['parts'] << no_link(page)
66
+ elsif page == 1
67
+ pagination['parts'] << link(page, page)
68
+ elsif page == page_count - 1
69
+ pagination['parts'] << link(page, page)
70
+ elsif page <= current_page - window_size or page >= current_page + window_size
71
+ next if hellip_break
72
+ pagination['parts'] << no_link('&hellip;')
73
+ hellip_break = true
74
+ next
75
+ else
76
+ pagination['parts'] << link(page, page)
77
+ end
78
+
79
+ hellip_break = false
80
+ end
81
+ end
82
+ context['paginate'] = pagination
83
+
84
+ super
85
+ end
86
+ end
87
+
88
+ private
89
+
90
+ def current_page
91
+ _current_page = @context.registers[:controller].params[:page]
92
+ _current_page.nil? ? 1 : _current_page.to_i
93
+ end
94
+
95
+ def current_url
96
+ current_url = @context.registers[:controller].request.fullpath.gsub(/page=[0-9]+&?/, '')
97
+ current_url = current_url.slice(0..-2) if current_url.last == '?' || current_url.last == '&'
98
+ current_url
99
+ end
100
+
101
+ def window_size
102
+ @attributes['window_size']
103
+ end
104
+
105
+ def no_link(title)
106
+ { 'title' => title, 'is_link' => false, 'hellip_break' => (title == '&hellip;') }
107
+ end
108
+
109
+ def link(title, page)
110
+ _current_url = %(#{current_url}#{current_url.include?('?') ? '&' : '?'}page=#{page})
111
+ { 'title' => title, 'url' => _current_url, 'is_link' => true }
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+ Liquid::Template.register_tag('paginate', Liquid::Rails::PaginateTag)
@@ -0,0 +1,44 @@
1
+ module Liquid
2
+ module Rails
3
+ class TemplateHandler
4
+
5
+ def self.call(template)
6
+ "Liquid::Rails::TemplateHandler.new(self).render(#{template.source.inspect}, local_assigns)"
7
+ end
8
+
9
+ def initialize(view)
10
+ @view = view
11
+ @controller = @view.controller
12
+ @helper = ActionController::Base.helpers
13
+ end
14
+
15
+ def render(template, local_assigns={})
16
+ @view.controller.headers['Content-Type'] ||= 'text/html; charset=utf-8'
17
+
18
+ assigns = if @controller.respond_to?(:liquid_assigns, true)
19
+ @controller.send(:liquid_assigns)
20
+ else
21
+ @view.assigns
22
+ end
23
+ assigns['content_for_layout'] = @view.content_for(:layout) if @view.content_for?(:layout)
24
+ assigns.merge!(local_assigns.stringify_keys)
25
+
26
+ liquid = Liquid::Template.parse(template)
27
+ render_method = (::Rails.env.development? || ::Rails.env.test?) ? :render! : :render
28
+ liquid.send(render_method, assigns, filters: filters, registers: { view: @view, controller: @controller, helper: @helper })
29
+ end
30
+
31
+ def filters
32
+ if @controller.respond_to?(:liquid_filters, true)
33
+ @controller.send(:liquid_filters)
34
+ else
35
+ [@controller._helpers]
36
+ end
37
+ end
38
+
39
+ def compilable?
40
+ false
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,5 @@
1
+ module Liquid
2
+ module Rails
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'liquid-rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'liquid-rails'
8
+ spec.version = Liquid::Rails::VERSION
9
+ spec.authors = ['Chamnap Chhorn']
10
+ spec.email = ['chamnapchhorn@gmail.com']
11
+ spec.summary = %q{Renders liquid templates with layout and partial support}
12
+ spec.description = %q{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.}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 2.0.0'
16
+ spec.required_rubygems_version = '>= 1.8.11'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'rails', '>= 3.2'
24
+ spec.add_dependency 'liquid', '~> 3.0.0.rc1'
25
+ spec.add_dependency 'kaminari', '~> 0.16.1'
26
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks
File without changes
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,25 @@
1
+ class HomeController < ApplicationController
2
+ before_filter :set_book
3
+
4
+ def index
5
+ render layout: false
6
+ end
7
+
8
+ def index_with_layout
9
+ end
10
+
11
+ def index_partial
12
+ end
13
+
14
+ def index_partial_with_full_path
15
+ end
16
+
17
+ def index_with_filter
18
+ end
19
+
20
+ private
21
+
22
+ def set_book
23
+ @book = { 'name' => 'Liquid on Rails' }
24
+ end
25
+ end
@@ -0,0 +1,16 @@
1
+ class PagesController < ApplicationController
2
+ before_filter :set_book
3
+
4
+ def index_witout_filter
5
+ end
6
+
7
+ private
8
+
9
+ def liquid_filters
10
+ []
11
+ end
12
+
13
+ def set_book
14
+ @book = { 'name' => 'Liquid on Rails' }
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module ApplicationHelper
2
+ def truncate_it(input, length)
3
+ input[0..length] + '...'
4
+ end
5
+ end
File without changes
File without changes
File without changes
@@ -0,0 +1 @@
1
+ Home Partial
@@ -0,0 +1 @@
1
+ {{ book.name }}
@@ -0,0 +1,4 @@
1
+ {{ book.name }}
2
+
3
+ {% include 'partial' %}
4
+ {% include 'shared/partial' %}
@@ -0,0 +1,4 @@
1
+ {{ book.name }}
2
+
3
+ {% include 'home/partial' %}
4
+ {% include 'shared/partial' %}
@@ -0,0 +1,2 @@
1
+ {{ book.name }}
2
+ {{ 'This is a long section of text' | truncate_it: 3 }}
@@ -0,0 +1 @@
1
+ {{ book.name }}
@@ -0,0 +1,2 @@
1
+ Application Layout
2
+ {{ content_for_layout }}
@@ -0,0 +1,2 @@
1
+ {{ book.name }}
2
+ {{ 'This is a long section of text' | truncate_it:3 }}
@@ -0,0 +1 @@
1
+ Shared Partial
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,25 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ # Pick the frameworks you want:
4
+ require "action_controller/railtie"
5
+ # require "rails/test_unit/railtie"
6
+
7
+ Bundler.require(*Rails.groups)
8
+ require "liquid-rails"
9
+
10
+ module Dummy
11
+ class Application < Rails::Application
12
+ # Settings in config/environments/* take precedence over those specified here.
13
+ # Application configuration should go into files in config/initializers
14
+ # -- all .rb files in that directory are automatically loaded.
15
+
16
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
17
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
18
+ # config.time_zone = 'Central Time (US & Canada)'
19
+
20
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
21
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
22
+ # config.i18n.default_locale = :de
23
+ end
24
+ end
25
+
@@ -0,0 +1,5 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
5
+ $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)