liquid4-rails5 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/liquid-rails/drops/collection_drop.rb +68 -66
- data/lib/liquid-rails/drops/drop.rb +1 -5
- data/lib/liquid-rails/drops/droppable.rb +3 -2
- data/lib/liquid-rails/railtie.rb +3 -3
- data/lib/liquid-rails/version.rb +1 -1
- data/spec/fixtures/poro.rb +2 -2
- data/spec/lib/liquid-rails/drops/drop_spec.rb +2 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 82899e24800c42e0d142c833ce84d9ae0012baa5
|
4
|
+
data.tar.gz: ab0d2c0849cfc67b5f97e5eb4d569ad4afcc731f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 817bd3f0050d0478804743da1f41309c8cd3a0f668220364dde427eb5585452316ff0e12325fb2785c1eead5ecd8233ab2cd5ad37bbe01385004f29d4e852a46
|
7
|
+
data.tar.gz: 4f9aa06f9577bc67250ea3175f00e44ba2161419bfc39b6f7beaea80ab65cd434c08e873daa92f2d3f70feabc2776661518f522518c04e9d64a500685568e227
|
@@ -1,89 +1,91 @@
|
|
1
|
-
module
|
2
|
-
|
1
|
+
module Liquid
|
2
|
+
module Rails
|
3
|
+
class CollectionDrop < ::Liquid::Drop
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
class << self
|
6
|
+
attr_accessor :_scopes
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
def self.inherited(base)
|
10
|
+
base._scopes = []
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
13
|
+
def self.scope(*scope_names)
|
14
|
+
@_scopes.concat scope_names
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
-
|
21
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
31
|
-
|
31
|
+
def initialize(objects, options={})
|
32
|
+
options.assert_valid_keys(:with, :scope)
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
34
|
+
@objects = options[:scope].nil? ? objects : objects.send(options[:scope])
|
35
|
+
@drop_class_name = options[:with]
|
36
|
+
end
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
|
38
|
+
def page(number)
|
39
|
+
self.class.new(objects.page(number))
|
40
|
+
end
|
40
41
|
|
41
|
-
|
42
|
-
|
43
|
-
|
42
|
+
def per(number)
|
43
|
+
self.class.new(objects.per(number))
|
44
|
+
end
|
44
45
|
|
45
|
-
|
46
|
-
|
47
|
-
|
46
|
+
def dropped_collection
|
47
|
+
@dropped_collection ||= @objects.map { |item| drop_item(item) }
|
48
|
+
end
|
48
49
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
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
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
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
|
-
|
73
|
-
|
74
|
-
|
73
|
+
def inspect
|
74
|
+
"#<#{self.class.name} of #{drop_class} for #{objects.inspect}>"
|
75
|
+
end
|
75
76
|
|
76
|
-
|
77
|
+
protected
|
77
78
|
|
78
|
-
|
79
|
+
attr_reader :objects
|
79
80
|
|
80
|
-
|
81
|
-
|
82
|
-
|
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
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
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
|
-
::
|
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
|
-
|
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
|
data/lib/liquid-rails/railtie.rb
CHANGED
@@ -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
|
-
|
21
|
+
if self == ActiveRecord::Base
|
22
22
|
Liquid::Rails.setup_drop(ActiveRecord::Relation)
|
23
|
-
|
23
|
+
end
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
data/lib/liquid-rails/version.rb
CHANGED
data/spec/fixtures/poro.rb
CHANGED
@@ -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(
|
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(
|
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(
|
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(
|
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
|
+
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-
|
11
|
+
date: 2017-03-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|