liquid4-rails5 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 177ab71e3e27da7d3f8d2a18d6db9e484c73593c
4
- data.tar.gz: a37feced734f5aec6a8b77b7d031ce7b96366c87
3
+ metadata.gz: 82899e24800c42e0d142c833ce84d9ae0012baa5
4
+ data.tar.gz: ab0d2c0849cfc67b5f97e5eb4d569ad4afcc731f
5
5
  SHA512:
6
- metadata.gz: 989abe139b73bd634f58bf15c08da8193f7525a8802c8f6d936ff55d832054d6e05f90eae9f80107b8a14bca0c53478d561132eb2e608452fcfec60ac01b8396
7
- data.tar.gz: 2dcfdabfe8f467e10734334dfe970317b29b62c5b3d53f0d0011bf8d391b6ff732361408a235e0dcabaecb9b9bfafe747a518cae65410b8f4bc5d844c7a4e827
6
+ metadata.gz: 817bd3f0050d0478804743da1f41309c8cd3a0f668220364dde427eb5585452316ff0e12325fb2785c1eead5ecd8233ab2cd5ad37bbe01385004f29d4e852a46
7
+ data.tar.gz: 4f9aa06f9577bc67250ea3175f00e44ba2161419bfc39b6f7beaea80ab65cd434c08e873daa92f2d3f70feabc2776661518f522518c04e9d64a500685568e227
@@ -1,89 +1,91 @@
1
- module ActiveRecord
2
- class RelationDrop < ::Liquid::Drop
1
+ module Liquid
2
+ module Rails
3
+ class CollectionDrop < ::Liquid::Drop
3
4
 
4
- class << self
5
- attr_accessor :_scopes
6
- end
5
+ class << self
6
+ attr_accessor :_scopes
7
+ end
7
8
 
8
- def self.inherited(base)
9
- base._scopes = []
10
- end
9
+ def self.inherited(base)
10
+ base._scopes = []
11
+ end
11
12
 
12
- def self.scope(*scope_names)
13
- @_scopes.concat scope_names
13
+ def self.scope(*scope_names)
14
+ @_scopes.concat scope_names
14
15
 
15
- scope_names.each do |scope_name|
16
- define_method(scope_name) do
17
- value = instance_variable_get("@_#{scope_name}")
18
- return value if value
16
+ scope_names.each do |scope_name|
17
+ define_method(scope_name) do
18
+ value = instance_variable_get("@_#{scope_name}")
19
+ return value if value
19
20
 
20
- raise ::Liquid::ArgumentError, "#{objects.class.name} doesn't define scope: #{scope_name}" unless objects.respond_to?(scope_name)
21
- instance_variable_set("@_#{scope_name}", self.class.new(objects.send(scope_name)))
21
+ raise ::Liquid::ArgumentError, "#{objects.class.name} doesn't define scope: #{scope_name}" unless objects.respond_to?(scope_name)
22
+ instance_variable_set("@_#{scope_name}", self.class.new(objects.send(scope_name)))
23
+ end
22
24
  end
23
25
  end
24
- end
25
26
 
26
- array_methods = Array.instance_methods - Object.instance_methods
27
- delegate *array_methods, to: :dropped_collection
28
- delegate :total_count, :total_pages, to: :objects
27
+ array_methods = Array.instance_methods - Object.instance_methods
28
+ delegate *array_methods, to: :dropped_collection
29
+ delegate :total_count, :total_pages, to: :objects
29
30
 
30
- def initialize(objects, options={})
31
- options.assert_valid_keys(:with, :scope)
31
+ def initialize(objects, options={})
32
+ options.assert_valid_keys(:with, :scope)
32
33
 
33
- @objects = options[:scope].nil? ? objects : objects.send(options[:scope])
34
- @drop_class_name = options[:with]
35
- end
34
+ @objects = options[:scope].nil? ? objects : objects.send(options[:scope])
35
+ @drop_class_name = options[:with]
36
+ end
36
37
 
37
- def page(number)
38
- self.class.new(objects.page(number))
39
- end
38
+ def page(number)
39
+ self.class.new(objects.page(number))
40
+ end
40
41
 
41
- def per(number)
42
- self.class.new(objects.per(number))
43
- end
42
+ def per(number)
43
+ self.class.new(objects.per(number))
44
+ end
44
45
 
45
- def dropped_collection
46
- @dropped_collection ||= @objects.map { |item| drop_item(item) }
47
- end
46
+ def dropped_collection
47
+ @dropped_collection ||= @objects.map { |item| drop_item(item) }
48
+ end
48
49
 
49
- def kind_of?(klass)
50
- dropped_collection.kind_of?(klass) || super
51
- end
52
- alias_method :is_a?, :kind_of?
53
-
54
- ## :[] is invoked by parser before the actual. However, this method has the same name as array method.
55
- ## Override this, so it will work for both cases.
56
- ## {{ post_drop.comments[0] }}
57
- ## {{ post_drop.<other_methods> }}
58
- def [](method)
59
- if method.is_a?(Integer)
60
- dropped_collection.at(method)
61
- else
62
- public_send(method)
50
+ def kind_of?(klass)
51
+ dropped_collection.kind_of?(klass) || super
52
+ end
53
+ alias_method :is_a?, :kind_of?
54
+
55
+ ## :[] is invoked by parser before the actual. However, this method has the same name as array method.
56
+ ## Override this, so it will work for both cases.
57
+ ## {{ post_drop.comments[0] }}
58
+ ## {{ post_drop.<other_methods> }}
59
+ def [](method)
60
+ if method.is_a?(Integer)
61
+ dropped_collection.at(method)
62
+ else
63
+ public_send(method)
64
+ end
63
65
  end
64
- end
65
66
 
66
- ## Need to override this. I don't understand too, otherwise it will return an array of drop objects.
67
- ## Need to return self so that we can do chaining.
68
- def to_liquid
69
- self
70
- end
67
+ ## Need to override this. I don't understand too, otherwise it will return an array of drop objects.
68
+ ## Need to return self so that we can do chaining.
69
+ def to_liquid
70
+ self
71
+ end
71
72
 
72
- def inspect
73
- "#<#{self.class.name} of #{drop_class} for #{objects.inspect}>"
74
- end
73
+ def inspect
74
+ "#<#{self.class.name} of #{drop_class} for #{objects.inspect}>"
75
+ end
75
76
 
76
- protected
77
+ protected
77
78
 
78
- attr_reader :objects
79
+ attr_reader :objects
79
80
 
80
- def drop_class
81
- @drop_class ||= @drop_class_name.is_a?(String) ? @drop_class_name.safe_constantize : @drop_class_name
82
- end
81
+ def drop_class
82
+ @drop_class ||= @drop_class_name.is_a?(String) ? @drop_class_name.safe_constantize : @drop_class_name
83
+ end
83
84
 
84
- def drop_item(item)
85
- liquid_drop_class = drop_class || item.drop_class
86
- liquid_drop_class.new(item)
87
- end
85
+ def drop_item(item)
86
+ liquid_drop_class = drop_class || item.drop_class
87
+ liquid_drop_class.new(item)
88
+ end
89
+ end
88
90
  end
89
91
  end
@@ -28,7 +28,7 @@ module Liquid
28
28
  # Array of products maps to `Liquid::Rails::CollectionDrop`.
29
29
  def self.drop_class_for(resource)
30
30
  if resource.respond_to?(:to_ary)
31
- ::ActiveRecord::RelationDrop
31
+ Liquid::Rails::CollectionDrop
32
32
  else
33
33
  if self == Liquid::Rails::Drop
34
34
  resource.drop_class
@@ -102,10 +102,6 @@ module Liquid
102
102
  as_json.to_json(options)
103
103
  end
104
104
 
105
- def before_method(method)
106
- attributes[method.to_s]
107
- end
108
-
109
105
  def liquid_method_missing(method)
110
106
  return nil unless @context && @context.strict_variables
111
107
  raise ::Liquid::UndefinedDropMethod, <<~HEREDOC
@@ -14,8 +14,9 @@ module Liquid
14
14
 
15
15
  module ClassMethods
16
16
  def drop_class
17
- if self.name == 'ActiveRecord::Associations::CollectionProxy'
18
- ActiveRecord::RelationDrop
17
+ if self.name == 'ActiveRecord::Associations::CollectionProxy' ||
18
+ self.name == 'ActiveRecord::AssociationRelation'
19
+ Liquid::Rails::CollectionDrop
19
20
  else
20
21
  "#{self.name}Drop".constantize
21
22
  end
@@ -11,16 +11,16 @@ module Liquid
11
11
 
12
12
  initializer 'liquid-rails.include_partial' do |app|
13
13
  template_path = ::Rails.root.join('app/views')
14
- Liquid::Template.file_system = Liquid::Rails::FileSystem.new(template_path)
14
+ ::Liquid::Template.file_system = Liquid::Rails::FileSystem.new(template_path)
15
15
  end
16
16
 
17
17
  initializer 'liquid-rails.setup_drop' do |app|
18
18
  [:active_record, :mongoid].each do |orm|
19
19
  ActiveSupport.on_load(orm) do
20
20
  Liquid::Rails.setup_drop(self)
21
- # if self.is_a? ActiveRecord::Base
21
+ if self == ActiveRecord::Base
22
22
  Liquid::Rails.setup_drop(ActiveRecord::Relation)
23
- # end
23
+ end
24
24
  end
25
25
  end
26
26
  end
@@ -1,5 +1,5 @@
1
1
  module Liquid
2
2
  module Rails
3
- VERSION = '0.4.0'
3
+ VERSION = '0.5.0'
4
4
  end
5
5
  end
@@ -52,7 +52,7 @@ CommentDrop = Class.new(Liquid::Rails::Drop) do
52
52
  end
53
53
 
54
54
  ReProfileDrop = Class.new(Liquid::Rails::Drop)
55
- PostsDrop = Class.new(ActiveRecord::RelationDrop)
55
+ PostsDrop = Class.new(Liquid::Rails::CollectionDrop)
56
56
  RePostDrop = Class.new(Liquid::Rails::Drop)
57
57
  ReCommentDrop = Class.new(Liquid::Rails::Drop)
58
- CommentsDrop = Class.new(ActiveRecord::RelationDrop)
58
+ CommentsDrop = Class.new(Liquid::Rails::CollectionDrop)
@@ -16,11 +16,6 @@ module Liquid
16
16
  expect(profile_drop.name).to eq('Name 1')
17
17
  expect(profile_drop.description).to eq('Description 1')
18
18
  end
19
-
20
- it '#before_method' do
21
- expect(profile_drop.before_method(:name)).to eq(profile_drop.name)
22
- expect(profile_drop.before_method(:description)).to eq(profile_drop.description)
23
- end
24
19
  end
25
20
 
26
21
  context '#dropify' do
@@ -49,7 +44,7 @@ module Liquid
49
44
  it "instantitates with collection drop class" do
50
45
  array = [1, 2, 3]
51
46
 
52
- expect(Liquid::Rails::Drop.dropify(array)).to be_instance_of(ActiveRecord::RelationDrop)
47
+ expect(Liquid::Rails::Drop.dropify(array)).to be_instance_of(Liquid::Rails::CollectionDrop)
53
48
  end
54
49
  end
55
50
  end
@@ -74,7 +69,7 @@ module Liquid
74
69
  end
75
70
 
76
71
  it '#comments returns as CollectionDrop object' do
77
- expect(@post_drop.comments).to be_instance_of(ActiveRecord::RelationDrop)
72
+ expect(@post_drop.comments).to be_instance_of(Liquid::Rails::CollectionDrop)
78
73
  end
79
74
 
80
75
  it '#recomments returns as CommentsDrop object' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: liquid4-rails5
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Viktor Fonic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-20 00:00:00.000000000 Z
11
+ date: 2017-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails