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,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,159 @@
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][:with] == options[:with] if options[:with]
75
+ result = result && association[:options][:class_name] == options[:class_name] if options[:class_name]
76
+
77
+ result
78
+ end
79
+
80
+ def with(class_name)
81
+ @options[:with] = class_name
82
+ self
83
+ end
84
+
85
+ def class_name(class_name)
86
+ @options[:class_name] = class_name
87
+ self
88
+ end
89
+
90
+ def description
91
+ "have association #{name}"
92
+ end
93
+
94
+ def failure_message
95
+ %Q{expected #{actual.inspect} to define "#{name}" as :#{options[:type]} association}
96
+ end
97
+
98
+ def failure_message_when_negated
99
+ %Q{expected #{actual.inspect} not to define "#{name}" as :#{options[:type]} association}
100
+ end
101
+
102
+ private
103
+
104
+ def drop
105
+ if actual.is_a?(Class)
106
+ actual
107
+ else
108
+ actual.class
109
+ end
110
+ end
111
+
112
+ def associations
113
+ drop._associations
114
+ end
115
+ end
116
+
117
+ class ScopeMatcher
118
+ attr_reader :name, :actual
119
+
120
+ def initialize(name)
121
+ @name = name
122
+ end
123
+
124
+ def matches?(actual)
125
+ @actual = actual
126
+
127
+ scopes.include?(name)
128
+ end
129
+
130
+ def description
131
+ "have scope #{name}"
132
+ end
133
+
134
+ def failure_message
135
+ %Q{expected #{actual.inspect} to define "#{name}" as scope}
136
+ end
137
+
138
+ def failure_message_when_negated
139
+ %Q{expected #{actual.inspect} not to define "#{name}" as scope}
140
+ end
141
+
142
+ private
143
+
144
+ def drop
145
+ if actual.is_a?(Class)
146
+ actual
147
+ else
148
+ actual.class
149
+ end
150
+ end
151
+
152
+ def scopes
153
+ drop._scopes
154
+ end
155
+ end
156
+ end
157
+ end
158
+ end
159
+ 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,44 @@
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 = ActionController::TestRequest.new
11
+ @response = ActionController::TestResponse.new
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
+ end
19
+
20
+ def view
21
+ @view
22
+ end
23
+
24
+ def controller
25
+ @controller
26
+ end
27
+
28
+ def context(assigns={})
29
+ @context ||= ::Liquid::Context.new(assigns, {}, { helper: @view, view: @view, controller: @controller })
30
+ end
31
+
32
+ def expect_template_result(template, expected, assigns={})
33
+ # make assigns available inside context
34
+ assigns.each do |key, value|
35
+ context[key] = value
36
+ end
37
+
38
+ actual = Liquid::Template.parse(template).render!(context)
39
+ expect(actual).to eq(expected)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,68 @@
1
+ # It works similar to Rails #content_for.
2
+ # Calling #content_for stores a block of markup in an identifier for later use.
3
+ # In order to access this stored content in other templates or the layout, you would pass the identifier as an argument to content_for.
4
+ #
5
+ # Usage:
6
+ #
7
+ # {% content_for not_authorized %}
8
+ # alert('You are not authorized to do that!');
9
+ # {% endcontent_for %}
10
+ #
11
+ # You can then use content_for :not_authorized anywhere in your templates.
12
+ # {% if current_user.nil? %}
13
+ # {% yield not_authorized %}
14
+ # {% endif %}
15
+
16
+ module Liquid
17
+ module Rails
18
+ class ContentForTag < ::Liquid::Block
19
+ Syntax = /(#{::Liquid::QuotedFragment}+)\s*(flush\s*(true|false))?/
20
+
21
+ def initialize(tag_name, markup, context)
22
+ super
23
+
24
+ if markup =~ Syntax
25
+ @identifier = $1.to_s
26
+ @flush = $3
27
+ else
28
+ raise SyntaxError.new("Syntax Error - Valid syntax: {% content_for [name] %}")
29
+ end
30
+ end
31
+
32
+ def render(context)
33
+ @context = context
34
+
35
+ options = @flush == 'true' ? { flush: true } : {}
36
+ @context.registers[:view].content_for(@identifier, super.html_safe, options)
37
+ ''
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ module Liquid
44
+ module Rails
45
+ class YieldTag < ::Liquid::Tag
46
+ Syntax = /(#{::Liquid::QuotedFragment}+)/
47
+
48
+ def initialize(tag_name, markup, context)
49
+ super
50
+
51
+ if markup =~ Syntax
52
+ @identifier = $1
53
+ else
54
+ raise SyntaxError.new("Syntax Error - Valid syntax: {% yield [name] %}")
55
+ end
56
+ end
57
+
58
+ def render(context)
59
+ @context = context
60
+
61
+ @context.registers[:view].content_for(@identifier).try(:html_safe)
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ Liquid::Template.register_tag('content_for', Liquid::Rails::ContentForTag)
68
+ Liquid::Template.register_tag('yield', Liquid::Rails::YieldTag)
@@ -0,0 +1,11 @@
1
+ module Liquid
2
+ module Rails
3
+ class CsrfMetaTags < ::Liquid::Tag
4
+ def render(context)
5
+ context.registers[:view].csrf_meta_tags
6
+ end
7
+ end
8
+ end
9
+ end
10
+
11
+ Liquid::Template.register_tag('csrf_meta_tags', Liquid::Rails::CsrfMetaTags)
@@ -0,0 +1,43 @@
1
+ # Returns a Google Analytics tag.
2
+ #
3
+ # Usage:
4
+ #
5
+ # {% google_analytics_tag 'UA-XXXXX-X' %}
6
+
7
+ module Liquid
8
+ module Rails
9
+ class GoogleAnalyticsTag < ::Liquid::Tag
10
+ Syntax = /(#{::Liquid::QuotedFragment}+)?/
11
+
12
+ def initialize(tag_name, markup, tokens)
13
+ if markup =~ Syntax
14
+ @account_id = $1.gsub('\'', '')
15
+ else
16
+ raise ::Liquid::SyntaxError.new("Syntax Error in 'google_analytics_tag' - Valid syntax: google_analytics_tag <account_id>")
17
+ end
18
+
19
+ super
20
+ end
21
+
22
+ def render(context)
23
+ %{
24
+ <script type="text/javascript">
25
+
26
+ var _gaq = _gaq || [];
27
+ _gaq.push(['_setAccount', '#{@account_id}']);
28
+ _gaq.push(['_trackPageview']);
29
+
30
+ (function() {
31
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
32
+ ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
33
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
34
+ })();
35
+
36
+ </script>
37
+ }
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ Liquid::Template.register_tag('google_analytics_tag', Liquid::Rails::GoogleAnalyticsTag)