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,114 @@
1
+ module Liquid
2
+ module Rails
3
+ class Drop < ::Liquid::Drop
4
+
5
+ class << self
6
+ attr_accessor :_attributes
7
+ attr_accessor :_associations
8
+ end
9
+
10
+ def self.inherited(base)
11
+ base._attributes = []
12
+ base._associations = {}
13
+ end
14
+
15
+ def self.attributes(*attrs)
16
+ @_attributes.concat attrs
17
+
18
+ attrs.each do |attr|
19
+ next if method_defined?(attr)
20
+
21
+ define_method(attr) do
22
+ object.send(attr) if object.respond_to?(attr, true)
23
+ end
24
+ end
25
+ end
26
+
27
+ # By default, `Product` maps to `ProductDrop`.
28
+ # Array of products maps to `Liquid::Rails::CollectionDrop`.
29
+ def self.drop_class_for(resource)
30
+ if resource.respond_to?(:to_ary)
31
+ Liquid::Rails::CollectionDrop
32
+ else
33
+ if self == Liquid::Rails::Drop
34
+ resource.drop_class || Liquid::Rails::Drop
35
+ else
36
+ self
37
+ end
38
+ end
39
+ end
40
+
41
+ # Create a drop instance when it cannot be inferred.
42
+ def self.dropify(resource, options={})
43
+ drop_class = if options[:class_name]
44
+ options[:class_name].constantize
45
+ else
46
+ drop_class_for(resource)
47
+ end
48
+
49
+ drop_class.new(resource, options.except(:class_name))
50
+ end
51
+
52
+ def self.belongs_to(*attrs)
53
+ associate(:belongs_to, attrs)
54
+ end
55
+
56
+ def self.has_many(*attrs)
57
+ associate(:has_many, attrs)
58
+ end
59
+
60
+ def self.associate(type, attrs)
61
+ options = attrs.extract_options!
62
+ self._associations = _associations.dup
63
+
64
+ attrs.each do |attr|
65
+ next if method_defined?(attr)
66
+
67
+ define_method attr do
68
+ value = instance_variable_get("@_#{attr}")
69
+ return value if value
70
+
71
+ association = object.send(attr)
72
+ return nil if association.nil?
73
+
74
+ drop_instance = Liquid::Rails::Drop.dropify(association, options)
75
+ instance_variable_set("@_#{attr}", drop_instance)
76
+ end
77
+
78
+ self._associations[attr] = { type: type, options: options }
79
+ end
80
+ end
81
+
82
+ # Wraps an object in a new instance of the drop.
83
+ def initialize(object, options={})
84
+ @object = object
85
+ end
86
+
87
+ def attributes
88
+ @attributes ||= self.class._attributes.dup.each_with_object({}) do |name, hash|
89
+ hash[name.to_s] = send(name)
90
+ end
91
+ end
92
+
93
+ def as_json(options={})
94
+ attributes.as_json(options)
95
+ end
96
+
97
+ def to_json(options={})
98
+ as_json.to_json(options)
99
+ end
100
+
101
+ def before_method(method)
102
+ attributes[method.to_s]
103
+ end
104
+
105
+ def inspect
106
+ "#<#{self.class.name} @object: #{object.inspect} @attributes: #{attributes.inspect}>"
107
+ end
108
+
109
+ protected
110
+
111
+ attr_reader :object
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,22 @@
1
+ module Liquid
2
+ module Rails
3
+ module Droppable
4
+ extend ActiveSupport::Concern
5
+
6
+ def to_liquid
7
+ drop_class.new(self)
8
+ end
9
+ alias_method :dropify, :to_liquid
10
+
11
+ def drop_class
12
+ self.class.drop_class
13
+ end
14
+
15
+ module ClassMethods
16
+ def drop_class
17
+ "#{self.name}Drop".safe_constantize
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ require 'liquid/file_system'
2
+
3
+ module Liquid
4
+ module Rails
5
+ class FileSystem < ::Liquid::LocalFileSystem
6
+ def read_template_file(template_path, context)
7
+ controller_name = context.registers[:controller].controller_name
8
+ template_path = "#{controller_name}/#{template_path}" unless template_path.include?('/')
9
+
10
+ super
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,25 @@
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
+ private
17
+
18
+ def h
19
+ @h ||= @context.registers[:view]
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ Liquid::Template.register_filter(Liquid::Rails::AssetTagFilter)
@@ -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,47 @@
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
+
26
+ def default_pagination(paginate)
27
+ html = []
28
+ html << %(<span class="prev">#{link_to(paginate['previous']['title'], paginate['previous']['url'])}</span>) if paginate['previous']
29
+
30
+ for part in paginate['parts']
31
+ if part['is_link']
32
+ html << %(<span class="page">#{link_to(part['title'], part['url'])}</span>)
33
+ elsif part['title'].to_i == paginate['current_page'].to_i
34
+ html << %(<span class="page current">#{part['title']}</span>)
35
+ else
36
+ html << %(<span class="deco">#{part['title']}</span>)
37
+ end
38
+ end
39
+
40
+ html << %(<span class="next">#{link_to(paginate['next']['title'], paginate['next']['url'])}</span>) if paginate['next']
41
+ html.join(' ')
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ 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,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,47 @@
1
+ module Liquid
2
+ module Rails
3
+ module TextFilter
4
+ delegate \
5
+ :truncate,
6
+ :highlight,
7
+ :excerpt,
8
+ :pluralize,
9
+ :word_wrap,
10
+ :simple_format,
11
+
12
+ to: :h
13
+
14
+ # right justify and padd a string
15
+ def rjust(input, integer, padstr = '')
16
+ input.to_s.rjust(integer, padstr)
17
+ end
18
+
19
+ # left justify and padd a string
20
+ def ljust(input, integer, padstr = '')
21
+ input.to_s.ljust(integer, padstr)
22
+ end
23
+
24
+ def underscore(input)
25
+ input.to_s.gsub(' ', '_').gsub('/', '_').underscore
26
+ end
27
+
28
+ def dasherize(input)
29
+ input.to_s.gsub(' ', '-').gsub('/', '-').dasherize
30
+ end
31
+
32
+ def concat(input, *args)
33
+ result = input.to_s
34
+ args.flatten.each { |a| result << a.to_s }
35
+ result
36
+ end
37
+
38
+ private
39
+
40
+ def h
41
+ @h ||= @context.registers[:view]
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ Liquid::Template.register_filter(Liquid::Rails::TextFilter)
@@ -0,0 +1,13 @@
1
+ module Liquid
2
+ module Rails
3
+ module TranslateFilter
4
+ def translate(key, locale = nil, scope = nil)
5
+ locale ||= ::I18n.locale.to_s
6
+
7
+ @context.registers[:view].translate(key.to_s, locale: locale, scope: scope)
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+ 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)