liquid4-rails5 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (119) 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-version +1 -0
  6. data/.travis.yml +22 -0
  7. data/CHANGELOG.md +36 -0
  8. data/Gemfile +12 -0
  9. data/Guardfile +33 -0
  10. data/LICENSE.txt +22 -0
  11. data/README.md +156 -0
  12. data/Rakefile +37 -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/gemfiles/rails_42.gemfile +11 -0
  17. data/lib/liquid-rails/drops/collection_drop.rb +91 -0
  18. data/lib/liquid-rails/drops/drop.rb +130 -0
  19. data/lib/liquid-rails/drops/droppable.rb +22 -0
  20. data/lib/liquid-rails/file_system.rb +16 -0
  21. data/lib/liquid-rails/filters/asset_tag_filter.rb +23 -0
  22. data/lib/liquid-rails/filters/asset_url_filter.rb +35 -0
  23. data/lib/liquid-rails/filters/date_filter.rb +17 -0
  24. data/lib/liquid-rails/filters/google_static_map_url_filter.rb +30 -0
  25. data/lib/liquid-rails/filters/misc_filter.rb +29 -0
  26. data/lib/liquid-rails/filters/number_filter.rb +22 -0
  27. data/lib/liquid-rails/filters/paginate_filter.rb +59 -0
  28. data/lib/liquid-rails/filters/sanitize_filter.rb +17 -0
  29. data/lib/liquid-rails/filters/text_filter.rb +44 -0
  30. data/lib/liquid-rails/filters/translate_filter.rb +14 -0
  31. data/lib/liquid-rails/filters/url_filter.rb +24 -0
  32. data/lib/liquid-rails/liquid_monkey_patch.rb +106 -0
  33. data/lib/liquid-rails/matchers.rb +5 -0
  34. data/lib/liquid-rails/railtie.rb +26 -0
  35. data/lib/liquid-rails/rspec/drop_example_group.rb +38 -0
  36. data/lib/liquid-rails/rspec/drop_matchers.rb +165 -0
  37. data/lib/liquid-rails/rspec/filter_example_group.rb +24 -0
  38. data/lib/liquid-rails/rspec/tag_example_group.rb +24 -0
  39. data/lib/liquid-rails/rspec/view_controller_context.rb +45 -0
  40. data/lib/liquid-rails/tags/content_for_tag.rb +77 -0
  41. data/lib/liquid-rails/tags/csrf_meta_tags.rb +11 -0
  42. data/lib/liquid-rails/tags/google_analytics_tag.rb +40 -0
  43. data/lib/liquid-rails/tags/javascript_tag.rb +22 -0
  44. data/lib/liquid-rails/tags/paginate_tag.rb +118 -0
  45. data/lib/liquid-rails/template_handler.rb +44 -0
  46. data/lib/liquid-rails/version.rb +5 -0
  47. data/lib/liquid-rails.rb +24 -0
  48. data/liquid-rails.gemspec +26 -0
  49. data/spec/dummy/README.rdoc +28 -0
  50. data/spec/dummy/Rakefile +6 -0
  51. data/spec/dummy/app/assets/images/.keep +0 -0
  52. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  53. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  54. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  55. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  56. data/spec/dummy/app/controllers/foospace/bar_controller.rb +5 -0
  57. data/spec/dummy/app/controllers/home_controller.rb +28 -0
  58. data/spec/dummy/app/controllers/pages_controller.rb +16 -0
  59. data/spec/dummy/app/helpers/application_helper.rb +5 -0
  60. data/spec/dummy/app/mailers/.keep +0 -0
  61. data/spec/dummy/app/models/.keep +0 -0
  62. data/spec/dummy/app/models/concerns/.keep +0 -0
  63. data/spec/dummy/app/views/foospace/bar/_partial.liquid +1 -0
  64. data/spec/dummy/app/views/foospace/bar/index_partial.liquid +3 -0
  65. data/spec/dummy/app/views/home/_html_liquid_partial.liquid +1 -0
  66. data/spec/dummy/app/views/home/_partial.liquid +1 -0
  67. data/spec/dummy/app/views/home/erb_with_html_liquid_partial.erb +1 -0
  68. data/spec/dummy/app/views/home/index.liquid +1 -0
  69. data/spec/dummy/app/views/home/index_partial.liquid +4 -0
  70. data/spec/dummy/app/views/home/index_partial_with_full_path.liquid +4 -0
  71. data/spec/dummy/app/views/home/index_with_filter.liquid +2 -0
  72. data/spec/dummy/app/views/home/index_with_layout.liquid +1 -0
  73. data/spec/dummy/app/views/layouts/application.liquid +2 -0
  74. data/spec/dummy/app/views/pages/index_without_filter.liquid +2 -0
  75. data/spec/dummy/app/views/shared/_partial.liquid +1 -0
  76. data/spec/dummy/bin/bundle +3 -0
  77. data/spec/dummy/bin/rails +4 -0
  78. data/spec/dummy/bin/rake +4 -0
  79. data/spec/dummy/config/application.rb +25 -0
  80. data/spec/dummy/config/boot.rb +5 -0
  81. data/spec/dummy/config/environment.rb +5 -0
  82. data/spec/dummy/config/environments/development.rb +29 -0
  83. data/spec/dummy/config/environments/production.rb +80 -0
  84. data/spec/dummy/config/environments/test.rb +36 -0
  85. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  86. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  87. data/spec/dummy/config/initializers/inflections.rb +16 -0
  88. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  89. data/spec/dummy/config/initializers/secret_token.rb +17 -0
  90. data/spec/dummy/config/initializers/session_store.rb +3 -0
  91. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  92. data/spec/dummy/config/locales/en.yml +5 -0
  93. data/spec/dummy/config/locales/km.yml +5 -0
  94. data/spec/dummy/config/routes.rb +14 -0
  95. data/spec/dummy/config.ru +4 -0
  96. data/spec/dummy/lib/assets/.keep +0 -0
  97. data/spec/dummy/log/.keep +0 -0
  98. data/spec/dummy/public/404.html +58 -0
  99. data/spec/dummy/public/422.html +58 -0
  100. data/spec/dummy/public/500.html +57 -0
  101. data/spec/dummy/public/favicon.ico +0 -0
  102. data/spec/fixtures/poro.rb +58 -0
  103. data/spec/lib/liquid-rails/drops/drop_spec.rb +119 -0
  104. data/spec/lib/liquid-rails/filters/asset_tag_filter_spec.rb +22 -0
  105. data/spec/lib/liquid-rails/filters/asset_url_filter_spec.rb +34 -0
  106. data/spec/lib/liquid-rails/filters/google_static_map_url_filter_spec.rb +23 -0
  107. data/spec/lib/liquid-rails/filters/misc_filter_spec.rb +26 -0
  108. data/spec/lib/liquid-rails/filters/translate_filter_spec.rb +29 -0
  109. data/spec/lib/liquid-rails/railtie_spec.rb +9 -0
  110. data/spec/lib/liquid-rails/rspec/drop_matchers_spec.rb +34 -0
  111. data/spec/lib/liquid-rails/tags/content_for_tag_spec.rb +33 -0
  112. data/spec/lib/liquid-rails/tags/csrf_meta_tag_spec.rb +25 -0
  113. data/spec/lib/liquid-rails/tags/google_analtyics_tag_spec.rb +28 -0
  114. data/spec/lib/liquid-rails/tags/javascript_tag_spec.rb +14 -0
  115. data/spec/lib/liquid-rails/tags/paginate_tag_spec.rb +160 -0
  116. data/spec/lib/liquid-rails/template_handler_spec.rb +67 -0
  117. data/spec/spec_helper.rb +42 -0
  118. data/spec/support/delegate_matcher.rb +60 -0
  119. metadata +274 -0
@@ -0,0 +1,23 @@
1
+ module Liquid
2
+ module Rails
3
+ module AssetTagFilter
4
+ delegate \
5
+ :audio_tag,
6
+ :auto_discovery_link_tag,
7
+ :favicon_link_tag,
8
+ :image_alt,
9
+ :image_tag,
10
+ :javascript_include_tag,
11
+ :stylesheet_link_tag,
12
+ :video_tag,
13
+
14
+ to: :h
15
+
16
+ def h
17
+ @h ||= @context.registers[:view]
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ Liquid::Template.register_filter(Liquid::Rails::AssetTagFilter)
@@ -0,0 +1,35 @@
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
+ def h
29
+ @h ||= @context.registers[:view]
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ Liquid::Template.register_filter(Liquid::Rails::AssetUrlFilter)
@@ -0,0 +1,17 @@
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
+ def h
11
+ @h ||= @context.registers[:view]
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ 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,22 @@
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
+ def h
16
+ @h ||= @context.registers[:view]
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ 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,17 @@
1
+ module Liquid
2
+ module Rails
3
+ module SanitizeFilter
4
+ delegate \
5
+ :strip_tags,
6
+ :strip_links,
7
+
8
+ to: :h
9
+
10
+ def h
11
+ @h ||= @context.registers[:view]
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ Liquid::Template.register_filter(Liquid::Rails::SanitizeFilter)
@@ -0,0 +1,44 @@
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
+ def h
38
+ @h ||= @context.registers[:view]
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ 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,106 @@
1
+ module Liquid
2
+ # A Liquid file system is a way to let your templates retrieve other templates for use with the include tag.
3
+ #
4
+ # You can implement subclasses that retrieve templates from the database, from the file system using a different
5
+ # path structure, you can provide them as hard-coded inline strings, or any manner that you see fit.
6
+ #
7
+ # You can add additional instance variables, arguments, or methods as needed.
8
+ #
9
+ # Example:
10
+ #
11
+ # Liquid::Template.file_system = Liquid::LocalFileSystem.new(template_path)
12
+ # liquid = Liquid::Template.parse(template)
13
+ #
14
+ # This will parse the template with a LocalFileSystem implementation rooted at 'template_path'.
15
+ class BlankFileSystem
16
+ # Called by Liquid to retrieve a template file
17
+ def read_template_file(_template_path, _context)
18
+ raise FileSystemError, "This liquid context does not allow includes."
19
+ end
20
+ end
21
+
22
+ # Context keeps the variable stack and resolves variables, as well as keywords
23
+ #
24
+ # context['variable'] = 'testing'
25
+ # context['variable'] #=> 'testing'
26
+ # context['true'] #=> true
27
+ # context['10.2232'] #=> 10.2232
28
+ #
29
+ # context.stack do
30
+ # context['bob'] = 'bobsen'
31
+ # end
32
+ #
33
+ # context['bob'] #=> nil class Context
34
+ class Context
35
+ # Fetches an object starting at the local scope and then moving up the hierachy
36
+ def find_variable(key)
37
+ # This was changed from find() to find_index() because this is a very hot
38
+ # path and find_index() is optimized in MRI to reduce object allocation
39
+ index = @scopes.find_index { |s| s.key?(key) }
40
+ scope = @scopes[index] if index
41
+
42
+ variable = nil
43
+
44
+ if scope.nil?
45
+ variable_is_nil = @environments.any? { |e| e.has_key?(key) && e[key].nil? }
46
+ unless variable_is_nil
47
+ @environments.each do |e|
48
+ variable = lookup_and_evaluate(e, key)
49
+ unless variable.nil?
50
+ scope = e
51
+ break
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ scope ||= @environments.last || @scopes.last
58
+ variable ||= lookup_and_evaluate(scope, key) unless variable_is_nil
59
+
60
+ variable = variable.to_liquid
61
+ variable.context = self if variable.respond_to?(:context=)
62
+
63
+ variable
64
+ end
65
+ end
66
+
67
+ class Include < Tag
68
+ def read_template_from_file_system(context)
69
+ @variable_name_expr = ''
70
+ file_system = context.registers[:file_system] || Liquid::Template.file_system
71
+
72
+ file_system.read_template_file(context.evaluate(@template_name_expr), context)
73
+ end
74
+ end
75
+
76
+ # This implements an abstract file system which retrieves template files named in a manner similar to Rails partials,
77
+ # ie. with the template name prefixed with an underscore. The extension ".liquid" is also added.
78
+ #
79
+ # For security reasons, template paths are only allowed to contain letters, numbers, and underscore.
80
+ #
81
+ # Example:
82
+ #
83
+ # file_system = Liquid::LocalFileSystem.new("/some/path")
84
+ #
85
+ # file_system.full_path("mypartial") # => "/some/path/_mypartial.liquid"
86
+ # file_system.full_path("dir/mypartial") # => "/some/path/dir/_mypartial.liquid"
87
+ #
88
+ # Optionally in the second argument you can specify a custom pattern for template filenames.
89
+ # The Kernel::sprintf format specification is used.
90
+ # Default pattern is "_%s.liquid".
91
+ #
92
+ # Example:
93
+ #
94
+ # file_system = Liquid::LocalFileSystem.new("/some/path", "%s.html")
95
+ #
96
+ # file_system.full_path("index") # => "/some/path/index.html"
97
+ #
98
+ class LocalFileSystem
99
+ def read_template_file(template_path, _context)
100
+ full_path = full_path(template_path)
101
+ raise FileSystemError, "No such template '#{template_path}'" unless File.exist?(full_path)
102
+
103
+ File.read(full_path)
104
+ end
105
+ end
106
+ end
@@ -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