taktsoft_liquid-rails 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) 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 +28 -0
  8. data/CHANGELOG.md +44 -0
  9. data/Gemfile +4 -0
  10. data/Guardfile +33 -0
  11. data/LICENSE.txt +22 -0
  12. data/README.md +152 -0
  13. data/Rakefile +25 -0
  14. data/gemfiles/rails_32.gemfile +9 -0
  15. data/gemfiles/rails_40.gemfile +8 -0
  16. data/gemfiles/rails_41.gemfile +8 -0
  17. data/gemfiles/rails_42.gemfile +8 -0
  18. data/gemfiles/rails_50.gemfile +5 -0
  19. data/lib/liquid-rails/drops/collection_drop.rb +91 -0
  20. data/lib/liquid-rails/drops/drop.rb +114 -0
  21. data/lib/liquid-rails/drops/droppable.rb +22 -0
  22. data/lib/liquid-rails/file_system.rb +13 -0
  23. data/lib/liquid-rails/filters/asset_tag_filter.rb +25 -0
  24. data/lib/liquid-rails/filters/asset_url_filter.rb +37 -0
  25. data/lib/liquid-rails/filters/date_filter.rb +19 -0
  26. data/lib/liquid-rails/filters/google_static_map_url_filter.rb +30 -0
  27. data/lib/liquid-rails/filters/misc_filter.rb +29 -0
  28. data/lib/liquid-rails/filters/number_filter.rb +24 -0
  29. data/lib/liquid-rails/filters/paginate_filter.rb +59 -0
  30. data/lib/liquid-rails/filters/sanitize_filter.rb +19 -0
  31. data/lib/liquid-rails/filters/text_filter.rb +46 -0
  32. data/lib/liquid-rails/filters/translate_filter.rb +14 -0
  33. data/lib/liquid-rails/filters/url_filter.rb +24 -0
  34. data/lib/liquid-rails/matchers.rb +5 -0
  35. data/lib/liquid-rails/railtie.rb +26 -0
  36. data/lib/liquid-rails/rspec/drop_example_group.rb +38 -0
  37. data/lib/liquid-rails/rspec/drop_matchers.rb +165 -0
  38. data/lib/liquid-rails/rspec/filter_example_group.rb +24 -0
  39. data/lib/liquid-rails/rspec/tag_example_group.rb +24 -0
  40. data/lib/liquid-rails/rspec/view_controller_context.rb +63 -0
  41. data/lib/liquid-rails/tags/content_for_tag.rb +77 -0
  42. data/lib/liquid-rails/tags/csrf_meta_tags.rb +11 -0
  43. data/lib/liquid-rails/tags/google_analytics_tag.rb +40 -0
  44. data/lib/liquid-rails/tags/javascript_tag.rb +22 -0
  45. data/lib/liquid-rails/tags/paginate_tag.rb +118 -0
  46. data/lib/liquid-rails/template_handler.rb +44 -0
  47. data/lib/liquid-rails/version.rb +5 -0
  48. data/lib/liquid-rails.rb +23 -0
  49. data/liquid-rails.gemspec +43 -0
  50. metadata +262 -0
@@ -0,0 +1,37 @@
1
+ module Liquid
2
+ module Rails
3
+ module AssetUrlFilter
4
+ delegate \
5
+ :asset_path,
6
+ :asset_url,
7
+
8
+ :audio_path,
9
+ :audio_url,
10
+
11
+ :font_path,
12
+ :font_url,
13
+
14
+ :image_path,
15
+ :image_url,
16
+
17
+ :javascript_path,
18
+ :javascript_url,
19
+
20
+ :stylesheet_path,
21
+ :stylesheet_url,
22
+
23
+ :video_path,
24
+ :video_url,
25
+
26
+ to: :h
27
+
28
+ private
29
+
30
+ def h
31
+ @h ||= @context.registers[:view]
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ Liquid::Template.register_filter(Liquid::Rails::AssetUrlFilter)
@@ -0,0 +1,19 @@
1
+ module Liquid
2
+ module Rails
3
+ module DateFilter
4
+ delegate \
5
+ :distance_of_time_in_words,
6
+ :time_ago_in_words,
7
+
8
+ to: :h
9
+
10
+ private
11
+
12
+ def h
13
+ @h ||= @context.registers[:view]
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ Liquid::Template.register_filter(Liquid::Rails::DateFilter)
@@ -0,0 +1,30 @@
1
+ module Liquid
2
+ module Rails
3
+ module GoogleStaticMapUrlFilter
4
+
5
+ # size: '600x300'
6
+ #
7
+ # Available keys inside options
8
+ # center: '40.714728,-73.998672'
9
+ # zoom: 13
10
+ # maptype: 'roadmap', 'satellite', 'terrain', or 'hybrid'
11
+ # markers: an array of this 'color:blue|label:S|40.702147,-74.015794'
12
+ # or string with semicolon
13
+ def google_static_map_url(size, options={})
14
+ markers = options.delete('markers')
15
+ markers = if markers
16
+ markers = markers.split(';') if markers.is_a?(String)
17
+ markers.map { |marker| { markers: marker }.to_query }
18
+ else
19
+ ''
20
+ end
21
+ options = options.merge('size' => size)
22
+ querystring = [options.to_query, markers].delete_if { |value| value.blank? }.join('&')
23
+
24
+ "https://maps.googleapis.com/maps/api/staticmap?#{querystring}"
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ Liquid::Template.register_filter(Liquid::Rails::GoogleStaticMapUrlFilter)
@@ -0,0 +1,29 @@
1
+ require 'json'
2
+
3
+ module Liquid
4
+ module Rails
5
+ module MiscFilter
6
+ # Get the nth element of the passed in array
7
+ def index(array, position)
8
+ array.at(position) if array.respond_to?(:at)
9
+ end
10
+
11
+ def random(input)
12
+ rand(input.to_i)
13
+ end
14
+
15
+ def jsonify(object)
16
+ JSON.dump(object)
17
+ end
18
+
19
+ # If condition is true, the class_name is returned. Otherwise, it returns nil.
20
+ # class_name: css class name
21
+ # condition: boolean
22
+ def toggle_class_name(class_name, condition)
23
+ condition ? class_name : nil
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ Liquid::Template.register_filter(Liquid::Rails::MiscFilter)
@@ -0,0 +1,24 @@
1
+ module Liquid
2
+ module Rails
3
+ module NumberFilter
4
+ delegate \
5
+ :number_to_phone,
6
+ :number_to_currency,
7
+ :number_to_percentage,
8
+ :number_with_delimiter,
9
+ :number_with_precision,
10
+ :number_to_human_size,
11
+ :number_to_human,
12
+
13
+ to: :h
14
+
15
+ private
16
+
17
+ def h
18
+ @h ||= @context.registers[:view]
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ Liquid::Template.register_filter(Liquid::Rails::NumberFilter)
@@ -0,0 +1,59 @@
1
+ module Liquid
2
+ module Rails
3
+ module PaginateFilter
4
+ def default_pagination(paginate)
5
+ html = []
6
+ html << %(<span class="prev"><a href="#{paginate['previous']['url']}" rel="prev">#{paginate['previous']['title']}</a></span>) if paginate['previous']
7
+
8
+ for part in paginate['parts']
9
+ if part['is_link']
10
+ html << %(<span class="page">#{link_to(part['title'], part['url'])}</span>)
11
+ elsif part['title'].to_i == paginate['current_page'].to_i
12
+ html << %(<span class="page current">#{part['title']}</span>)
13
+ else
14
+ html << %(<span class="deco">#{part['title']}</span>)
15
+ end
16
+ end
17
+
18
+ html << %(<span class="next"><a href="#{paginate['next']['url']}" rel="next">#{paginate['next']['title']}</a></span>) if paginate['next']
19
+ html.join(' ')
20
+ end
21
+
22
+ # Bootstrap pagination filter
23
+ #
24
+ # @param [ paginate ]
25
+ # @param [ size ]: .pagination-lg, .pagination-sm
26
+ def bootstrap_pagination(paginate, size='')
27
+ html = []
28
+ html << %{<nav><ul class="pagination #{size}">}
29
+
30
+ if paginate['previous']
31
+ html << %(<li><a href="#{paginate['previous']['url']}" aria-label="Previous"><span aria-hidden="true">#{paginate['previous']['title']}</span></a></li>)
32
+ else
33
+ html << %(<li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">&laquo; Previous</span></a></li>)
34
+ end
35
+
36
+ for part in paginate['parts']
37
+ if part['is_link']
38
+ html << %(<li><a href="#{part['url']}">#{part['title']}</a></li>)
39
+ elsif part['title'].to_i == paginate['current_page'].to_i
40
+ html << %(<li class="active"><span>#{part['title']}</span></li>)
41
+ else
42
+ html << %(<li class="disabled"><span>#{part['title']}</span></li>)
43
+ end
44
+ end
45
+
46
+ if paginate['next']
47
+ html << %(<li><a href="#{paginate['next']['url']}" aria-label="Next"><span aria-hidden="true">#{paginate['next']['title']}</span></a></li>)
48
+ else
49
+ html << %(<li class="disabled"><a href="#" aria-label="Next"><span aria-hidden="true">Next &raquo;</span></a></li>)
50
+ end
51
+
52
+ html << '</ul></nav>'
53
+ html.join(' ')
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ Liquid::Template.register_filter(Liquid::Rails::PaginateFilter)
@@ -0,0 +1,19 @@
1
+ module Liquid
2
+ module Rails
3
+ module SanitizeFilter
4
+ delegate \
5
+ :strip_tags,
6
+ :strip_links,
7
+
8
+ to: :h
9
+
10
+ private
11
+
12
+ def h
13
+ @h ||= @context.registers[:view]
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ Liquid::Template.register_filter(Liquid::Rails::SanitizeFilter)
@@ -0,0 +1,46 @@
1
+ module Liquid
2
+ module Rails
3
+ module TextFilter
4
+ delegate \
5
+ :highlight,
6
+ :excerpt,
7
+ :pluralize,
8
+ :word_wrap,
9
+ :simple_format,
10
+
11
+ to: :h
12
+
13
+ # right justify and padd a string
14
+ def rjust(input, integer, padstr = '')
15
+ input.to_s.rjust(integer, padstr)
16
+ end
17
+
18
+ # left justify and padd a string
19
+ def ljust(input, integer, padstr = '')
20
+ input.to_s.ljust(integer, padstr)
21
+ end
22
+
23
+ def underscore(input)
24
+ input.to_s.gsub(' ', '_').gsub('/', '_').underscore
25
+ end
26
+
27
+ def dasherize(input)
28
+ input.to_s.gsub(' ', '-').gsub('/', '-').dasherize
29
+ end
30
+
31
+ def concat(input, *args)
32
+ result = input.to_s
33
+ args.flatten.each { |a| result << a.to_s }
34
+ result
35
+ end
36
+
37
+ private
38
+
39
+ def h
40
+ @h ||= @context.registers[:view]
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ Liquid::Template.register_filter(Liquid::Rails::TextFilter)
@@ -0,0 +1,14 @@
1
+ module Liquid
2
+ module Rails
3
+ module TranslateFilter
4
+ def translate(key, options={})
5
+ options = { 'locale' => ::I18n.locale.to_s }.merge(options)
6
+
7
+ @context.registers[:view].translate(key.to_s, options.with_indifferent_access)
8
+ end
9
+ alias_method :t, :translate
10
+ end
11
+ end
12
+ end
13
+
14
+ Liquid::Template.register_filter Liquid::Rails::TranslateFilter
@@ -0,0 +1,24 @@
1
+ module Liquid
2
+ module Rails
3
+ module UrlFilter
4
+
5
+ def link_to(name, url, options={})
6
+ @context.registers[:view].link_to(name, url.to_s, options)
7
+ end
8
+
9
+ def link_to_unless_current(name, url, options={})
10
+ @context.registers[:view].link_to_unless_current(name, url.to_s, options)
11
+ end
12
+
13
+ def mail_to(email_address, name=nil, options={})
14
+ @context.registers[:view].mail_to(email_address, name, options)
15
+ end
16
+
17
+ def current_page?(path)
18
+ @context.registers[:view].current_page?(path.to_s)
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ Liquid::Template.register_filter(Liquid::Rails::UrlFilter)
@@ -0,0 +1,5 @@
1
+ require 'liquid-rails/rspec/view_controller_context'
2
+ require 'liquid-rails/rspec/drop_matchers'
3
+ require 'liquid-rails/rspec/drop_example_group'
4
+ require 'liquid-rails/rspec/tag_example_group'
5
+ require 'liquid-rails/rspec/filter_example_group'
@@ -0,0 +1,26 @@
1
+ module Liquid
2
+ module Rails
3
+ class Railtie < ::Rails::Railtie
4
+ config.app_generators.template_engine :liquid
5
+
6
+ initializer 'liquid-rails.register_template_handler' do |app|
7
+ ActiveSupport.on_load(:action_view) do
8
+ ActionView::Template.register_template_handler(:liquid, Liquid::Rails::TemplateHandler)
9
+ end
10
+ end
11
+
12
+ initializer 'liquid-rails.include_partial' do |app|
13
+ template_path = ::Rails.root.join('app/views')
14
+ Liquid::Template.file_system = Liquid::Rails::FileSystem.new(template_path)
15
+ end
16
+
17
+ initializer 'liquid-rails.setup_drop' do |app|
18
+ [:active_record, :mongoid].each do |orm|
19
+ ActiveSupport.on_load orm do
20
+ Liquid::Rails.setup_drop self
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,38 @@
1
+ module Liquid
2
+ module Rails
3
+ module Rspec
4
+ module DropExampleGroup
5
+ extend ActiveSupport::Concern
6
+ include Liquid::Rails::Rspec::DropMatchers
7
+ include Liquid::Rails::Rspec::ViewControllerContext
8
+
9
+ included do
10
+ metadata[:type] = :drop
11
+
12
+ subject {
13
+ if described_class.ancestors.include?(Liquid::Rails::Drop)
14
+ begin
15
+ described_class.new(double)
16
+ rescue
17
+ described_class.new
18
+ end
19
+ else
20
+ described_class.new([])
21
+ end
22
+ }
23
+
24
+ before(:all) { setup_view_and_controller }
25
+ before(:each) { subject.context = context }
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ RSpec.configure do |config|
33
+ if RSpec::Core::Version::STRING.starts_with?('3')
34
+ config.include Liquid::Rails::Rspec::DropExampleGroup, type: :drop, file_path: %r(spec/drops)
35
+ else
36
+ config.include Liquid::Rails::Rspec::DropExampleGroup, type: :drop, example_group: { file_path: %r{spec/drops} }
37
+ end
38
+ end
@@ -0,0 +1,165 @@
1
+ module Liquid
2
+ module Rails
3
+ module Rspec
4
+ module DropMatchers
5
+ def have_attribute(name)
6
+ AttributeMatcher.new(name)
7
+ end
8
+
9
+ def have_many(name)
10
+ AssociationMatcher.new(name, type: :has_many)
11
+ end
12
+
13
+ def belongs_to(name)
14
+ AssociationMatcher.new(name, type: :belongs_to)
15
+ end
16
+
17
+ def have_scope(name)
18
+ ScopeMatcher.new(name)
19
+ end
20
+
21
+ class AttributeMatcher
22
+ attr_reader :name, :actual
23
+
24
+ def initialize(name)
25
+ @name = name
26
+ end
27
+
28
+ def matches?(actual)
29
+ @actual = actual
30
+
31
+ attributes.include?(name)
32
+ end
33
+
34
+ def description
35
+ "have attribute #{name}"
36
+ end
37
+
38
+ def failure_message
39
+ %Q{expected #{actual.inspect} to define "#{name}" as attribute}
40
+ end
41
+
42
+ def failure_message_when_negated
43
+ %Q{expected #{actual.inspect} not to define "#{name}" as attribute}
44
+ end
45
+
46
+ private
47
+
48
+ def drop
49
+ if actual.is_a?(Class)
50
+ actual
51
+ else
52
+ actual.class
53
+ end
54
+ end
55
+
56
+ def attributes
57
+ drop._attributes
58
+ end
59
+ end
60
+
61
+ class AssociationMatcher
62
+ attr_reader :name, :actual, :options
63
+
64
+ def initialize(name, options)
65
+ @name = name
66
+ @options = options
67
+ end
68
+
69
+ def matches?(actual)
70
+ @actual = actual
71
+
72
+ association = associations[name]
73
+ result = association.present? && association[:type] == options[:type]
74
+ result = result && association[:options][:scope] == options[:scope] if options[:scope]
75
+ result = result && association[:options][:with] == options[:with] if options[:with]
76
+ result = result && association[:options][:class_name] == options[:class_name] if options[:class_name]
77
+
78
+ result
79
+ end
80
+
81
+ def with(class_name)
82
+ @options[:with] = class_name
83
+ self
84
+ end
85
+
86
+ def scope(scope_name)
87
+ @options[:scope] = scope_name
88
+ self
89
+ end
90
+
91
+ def class_name(class_name)
92
+ @options[:class_name] = class_name
93
+ self
94
+ end
95
+
96
+ def description
97
+ "have association #{name}"
98
+ end
99
+
100
+ def failure_message
101
+ %Q{expected #{actual.inspect} to define "#{name}" as :#{options[:type]} association}
102
+ end
103
+
104
+ def failure_message_when_negated
105
+ %Q{expected #{actual.inspect} not to define "#{name}" as :#{options[:type]} association}
106
+ end
107
+
108
+ private
109
+
110
+ def drop
111
+ if actual.is_a?(Class)
112
+ actual
113
+ else
114
+ actual.class
115
+ end
116
+ end
117
+
118
+ def associations
119
+ drop._associations
120
+ end
121
+ end
122
+
123
+ class ScopeMatcher
124
+ attr_reader :name, :actual
125
+
126
+ def initialize(name)
127
+ @name = name
128
+ end
129
+
130
+ def matches?(actual)
131
+ @actual = actual
132
+
133
+ scopes.include?(name)
134
+ end
135
+
136
+ def description
137
+ "have scope #{name}"
138
+ end
139
+
140
+ def failure_message
141
+ %Q{expected #{actual.inspect} to define "#{name}" as scope}
142
+ end
143
+
144
+ def failure_message_when_negated
145
+ %Q{expected #{actual.inspect} not to define "#{name}" as scope}
146
+ end
147
+
148
+ private
149
+
150
+ def drop
151
+ if actual.is_a?(Class)
152
+ actual
153
+ else
154
+ actual.class
155
+ end
156
+ end
157
+
158
+ def scopes
159
+ drop._scopes
160
+ end
161
+ end
162
+ end
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,24 @@
1
+ module Liquid
2
+ module Rails
3
+ module Rspec
4
+ module FilterExampleGroup
5
+ extend ActiveSupport::Concern
6
+ include Liquid::Rails::Rspec::ViewControllerContext
7
+
8
+ included do
9
+ metadata[:type] = :filter
10
+
11
+ before(:all) { setup_view_and_controller }
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ RSpec.configure do |config|
19
+ if RSpec::Core::Version::STRING.starts_with?('3')
20
+ config.include Liquid::Rails::Rspec::FilterExampleGroup, type: :filter, file_path: %r(spec/filters)
21
+ else
22
+ config.include Liquid::Rails::Rspec::FilterExampleGroup, type: :filter, example_group: { file_path: %r{spec/filters} }
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ module Liquid
2
+ module Rails
3
+ module Rspec
4
+ module TagExampleGroup
5
+ extend ActiveSupport::Concern
6
+ include Liquid::Rails::Rspec::ViewControllerContext
7
+
8
+ included do
9
+ metadata[:type] = :tag
10
+
11
+ before(:all) { setup_view_and_controller }
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ RSpec.configure do |config|
19
+ if RSpec::Core::Version::STRING.starts_with?('3')
20
+ config.include Liquid::Rails::Rspec::TagExampleGroup, type: :tag, file_path: %r(spec/tags)
21
+ else
22
+ config.include Liquid::Rails::Rspec::TagExampleGroup, type: :tag, example_group: { file_path: %r{spec/tags} }
23
+ end
24
+ end
@@ -0,0 +1,63 @@
1
+ module Liquid
2
+ module Rails
3
+ module Rspec
4
+ module ViewControllerContext
5
+ extend ActiveSupport::Concern
6
+
7
+ def setup_view_and_controller
8
+ @view = ActionView::Base.new
9
+ @controller = ApplicationController.new
10
+ @request = build_request_for_liquid
11
+ @response = build_response_for_liquid
12
+ @response.request = @request
13
+ @controller.request = @request
14
+ @controller.response = @response
15
+ @controller.params = {}
16
+ @view.assign_controller(@controller)
17
+ @view.class.send(:include, @controller._helpers)
18
+ @view.class.send(:include, ::Rails.application.routes.url_helpers)
19
+ end
20
+
21
+ def view
22
+ @view
23
+ end
24
+
25
+ def controller
26
+ @controller
27
+ end
28
+
29
+ def context(assigns={})
30
+ @context ||= ::Liquid::Context.new(assigns, {}, { helper: @view, view: @view, controller: @controller })
31
+ end
32
+
33
+ def expect_template_result(template, expected, assigns={})
34
+ # make assigns available inside context
35
+ assigns.each do |key, value|
36
+ context[key] = value
37
+ end
38
+
39
+ actual = Liquid::Template.parse(template).render!(context)
40
+ expect(actual.to_s.strip).to eq(expected.to_s.strip)
41
+ end
42
+
43
+ private
44
+
45
+ def build_request_for_liquid
46
+ if ::Rails::VERSION::MAJOR < 5
47
+ ActionController::TestRequest.new({'PATH_INFO' => '/'})
48
+ else
49
+ ActionController::TestRequest.new({'PATH_INFO' => '/'}, {})
50
+ end
51
+ end
52
+
53
+ def build_response_for_liquid
54
+ if ::Rails::VERSION::MAJOR < 5
55
+ ActionController::TestResponse.new
56
+ else
57
+ ActionDispatch::TestResponse.new
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end