locomotive_plugins 1.0.0.beta4 → 1.0.0.beta5

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,21 +14,19 @@ module Locomotive
14
14
  @plugin.custom_attribute.should == 'Value'
15
15
  end
16
16
 
17
- it 'should call the gven block before custom initialization methods' do
17
+ it 'should call the given block before custom initialization methods' do
18
18
  @plugin = MyPlugin.new(@config) do |obj|
19
19
  obj.custom_attribute.should be_nil
20
20
  end
21
21
  @plugin.custom_attribute.should_not be_nil
22
22
  end
23
23
 
24
- it 'should store a list of before_filters' do
25
- @plugin.before_filters.count.should == 2
26
- @plugin.before_filters[0].should == :my_method1
27
- @plugin.before_filters[1].should == :my_method2
28
- end
29
-
30
- it 'should have an empty array of before_filters by default' do
31
- @useless_plugin.before_filters.should == []
24
+ it 'should have filter callbacks' do
25
+ @plugin.expects(:my_method1)
26
+ @plugin.expects(:my_method2)
27
+ @plugin.expects(:my_method3)
28
+ @plugin.run_callbacks(:filter) do
29
+ end
32
30
  end
33
31
 
34
32
  it 'should optionally return a liquid drop' do
@@ -42,7 +40,7 @@ module Locomotive
42
40
  end
43
41
 
44
42
  it 'should optionally return liquid tags' do
45
- MyPlugin.liquid_tags.should == {}
43
+ UselessPlugin.liquid_tags.should == {}
46
44
  PluginWithTags.liquid_tags.should == {
47
45
  :paragraph => PluginWithTags::Paragraph,
48
46
  :newline => PluginWithTags::Newline
@@ -4,13 +4,20 @@ module Locomotive
4
4
  include Locomotive::Plugin
5
5
 
6
6
  module Filters
7
+ def filter(input)
8
+ input
9
+ end
7
10
  end
8
11
 
9
12
  class MyDrop < ::Liquid::Drop
10
13
  end
11
14
 
15
+ class MyTag < ::Liquid::Tag
16
+ end
17
+
12
18
  before_filter :my_method1
13
- before_filter :my_method2
19
+ after_filter :my_method2
20
+ around_filter :my_method3
14
21
 
15
22
  attr_accessor :custom_attribute
16
23
 
@@ -31,12 +38,20 @@ module Locomotive
31
38
  Filters
32
39
  end
33
40
 
41
+ def self.liquid_tags
42
+ { 'my_tag' => MyTag }
43
+ end
44
+
34
45
  def my_method1
35
- 'This is my first before filter!'
46
+ 'This is my before filter!'
36
47
  end
37
48
 
38
49
  def my_method2
39
- 'This is my second before filter!'
50
+ 'This is my after filter!'
51
+ end
52
+
53
+ def my_method3
54
+ 'This is my around filter!'
40
55
  end
41
56
  end
42
57
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: locomotive_plugins
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.beta4
4
+ version: 1.0.0.beta5
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-10 00:00:00.000000000 Z
12
+ date: 2013-01-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: locomotive_liquid
@@ -115,12 +115,12 @@ extensions: []
115
115
  extra_rdoc_files: []
116
116
  files:
117
117
  - Rakefile
118
- - lib/locomotive/plugin/db_model_container.rb
119
118
  - lib/locomotive/plugin/liquid.rb
119
+ - lib/locomotive/plugin/class_tracker.rb
120
120
  - lib/locomotive/plugin/liquid/prefixed_filter_module.rb
121
+ - lib/locomotive/plugin/liquid/drop_extension.rb
121
122
  - lib/locomotive/plugin/liquid/tag_subclass_methods.rb
122
- - lib/locomotive/plugin/db_models.rb
123
- - lib/locomotive/plugin/db_model.rb
123
+ - lib/locomotive/plugin/liquid/context_helpers.rb
124
124
  - lib/locomotive/plugin/config_ui.rb
125
125
  - lib/locomotive/plugin.rb
126
126
  - lib/version.rb
@@ -133,16 +133,14 @@ files:
133
133
  - spec/support/plugins/plugin_with_filter.rb
134
134
  - spec/support/plugins/plugin_with_many_filter_modules.rb
135
135
  - spec/support/plugins/my_plugin.rb
136
- - spec/support/plugins/plugin_with_db_model_relationships.rb
137
- - spec/support/plugins/plugin_with_db_model.rb
138
136
  - spec/support/factories.rb
139
137
  - spec/fixtures/config_template.haml
140
138
  - spec/fixtures/config_template.html
139
+ - spec/lib/locomotive/plugin/class_tracker_spec.rb
140
+ - spec/lib/locomotive/plugin/liquid/context_helpers_spec.rb
141
141
  - spec/lib/locomotive/plugin/liquid_spec.rb
142
- - spec/lib/locomotive/plugin/db_models_spec.rb
143
142
  - spec/lib/locomotive/plugin/config_ui_spec.rb
144
143
  - spec/lib/locomotive/plugin_spec.rb
145
- - spec/lib/locomotive_plugins_spec.rb
146
144
  - README.md
147
145
  - CHANGELOG
148
146
  homepage: https://github.com/colibri-software/locomotive_plugins
@@ -1,10 +0,0 @@
1
-
2
- module Locomotive
3
- module Plugin
4
- # All classes to be persisted by a plugin should inherit from this class
5
- # (see Locomotive::Plugin::ClassMethods #has_many or #has_one)
6
- class DBModel
7
- include ::Mongoid::Document
8
- end
9
- end
10
- end
@@ -1,18 +0,0 @@
1
-
2
- module Locomotive
3
- module Plugin
4
- # The superclass of the DBModelContainer object for each plugin. This
5
- # container has the actual relationships to the database objects
6
- class DBModelContainer
7
- include Mongoid::Document
8
-
9
- field :container_name
10
-
11
- def self.for_name(container_name)
12
- where(container_name: container_name).first || \
13
- new(container_name: container_name)
14
- end
15
-
16
- end
17
- end
18
- end
@@ -1,111 +0,0 @@
1
-
2
- module Locomotive
3
- module Plugin
4
-
5
- # Utility methods for dealing with DB Models
6
- module DBModels
7
-
8
- # @private
9
- def self.included(base)
10
- base.extend ClassMethods
11
- end
12
-
13
- # @private
14
- module ClassMethods
15
-
16
- def add_db_model_class_methods(base)
17
- base.class_eval <<-CODE
18
- class DBModelContainer < ::Locomotive::Plugin::DBModelContainer
19
- end
20
-
21
- def self.db_model_container_class
22
- DBModelContainer
23
- end
24
- CODE
25
- base.extend DBModelClassMethods
26
- end
27
-
28
- end
29
-
30
- # @private
31
- module DBModelClassMethods
32
-
33
- protected
34
-
35
- def create_has_many_relationship(name, klass)
36
- self.db_model_container_class.has_many(name,
37
- class_name: klass.to_s, inverse_of: :db_model_container,
38
- autosave: true, dependent: :destroy)
39
- klass.belongs_to(:db_model_container,
40
- class_name: db_model_container_class.to_s, inverse_of: name)
41
-
42
- self.define_passthrough_methods_to_container(name, "#{name}=",
43
- "#{name}_ids", "#{name}_ids=")
44
- end
45
-
46
- def create_has_one_relationship(name, klass)
47
- self.db_model_container_class.has_one(name,
48
- class_name: klass.to_s, inverse_of: :db_model_container,
49
- autosave: true, dependent: :destroy)
50
- klass.belongs_to(:db_model_container,
51
- class_name: db_model_container_class.to_s, inverse_of: name)
52
-
53
- self.define_passthrough_methods_to_container(name, "#{name}=",
54
- "build_#{name}", "create_#{name}")
55
- end
56
-
57
- def define_passthrough_methods_to_container(*methods)
58
- class_eval do
59
- methods.each do |meth|
60
- define_method(meth) do |*args|
61
- db_model_container.send(meth, *args)
62
- end
63
- end
64
- end
65
- end
66
-
67
- end
68
-
69
- # Save the DB Model container
70
- def save_db_model_container
71
- self.db_model_container.save
72
- end
73
-
74
- # Get the DB Model container
75
- def db_model_container
76
- @db_model_containers ||= {}
77
- name = self.current_db_model_container_name
78
- @db_model_containers[name] ||= load_db_model_container(name)
79
- end
80
-
81
- # Set the current DB Model container
82
- def use_db_model_container(name)
83
- @current_db_model_container_name = name
84
- end
85
-
86
- # Reset to the default DB Model container
87
- def reset_current_db_model_container
88
- self.use_db_model_container(nil)
89
- end
90
-
91
- # Set the current DB Model container for the duration of the block
92
- def with_db_model_container(name, &block)
93
- old_name = self.current_db_model_container_name
94
- self.use_db_model_container(name)
95
- block.call
96
- self.use_db_model_container(old_name)
97
- end
98
-
99
- # The name of the current container being used
100
- attr_reader :current_db_model_container_name
101
-
102
- protected
103
-
104
- def load_db_model_container(name)
105
- self.class.db_model_container_class.for_name(name)
106
- end
107
-
108
- end
109
-
110
- end
111
- end
@@ -1,206 +0,0 @@
1
-
2
- module Locomotive
3
- module Plugin
4
- describe DBModels do
5
-
6
- before(:each) do
7
- plugin = PluginWithDBModel.new({})
8
- plugin.build_visit_count(count: 5)
9
- plugin.items.build(name: 'First Item')
10
- plugin.items.build(name: 'Second Item')
11
- plugin.save_db_model_container.should be_true
12
-
13
- # Reload from the database
14
- @plugin_with_db_model = PluginWithDBModel.new({})
15
- end
16
-
17
- it 'should persist DBModel items' do
18
- @plugin_with_db_model.visit_count.count.should == 5
19
-
20
- @plugin_with_db_model.items.count.should == 2
21
- @plugin_with_db_model.items[0].name.should == 'First Item'
22
- @plugin_with_db_model.items[1].name.should == 'Second Item'
23
- end
24
-
25
- it 'should allow mongoid queries on persisted DBModel items' do
26
- @plugin_with_db_model.items.where(name: /First/).count.should == 1
27
- @plugin_with_db_model.items.where(name: /First/).first.name.should == 'First Item'
28
-
29
- @plugin_with_db_model.items.where(name: /Item/).count.should == 2
30
- @plugin_with_db_model.items.where(name: /Item/)[0].name.should == 'First Item'
31
- @plugin_with_db_model.items.where(name: /Item/)[1].name.should == 'Second Item'
32
- end
33
-
34
- it 'should reference DBModel items in a document for the plugin class' do
35
- @plugin_with_db_model.visit_count.db_model_container.kind_of?(
36
- PluginWithDBModel::DBModelContainer).should be_true
37
- @plugin_with_db_model.items.first.db_model_container.kind_of?(
38
- PluginWithDBModel::DBModelContainer).should be_true
39
-
40
- @plugin_with_db_model.visit_count.relations[
41
- 'db_model_container'].relation.should \
42
- == Mongoid::Relations::Referenced::In
43
- @plugin_with_db_model.items.relations[
44
- 'db_model_container'].relation.should \
45
- == Mongoid::Relations::Referenced::In
46
- end
47
-
48
- it 'should run all validations for DBModel items' do
49
- @plugin_with_db_model.items.build(name: '')
50
- @plugin_with_db_model.save_db_model_container.should be_false
51
-
52
- @plugin_with_db_model.db_model_container.errors.messages.should == {
53
- :items => [ 'is invalid' ]
54
- }
55
- end
56
-
57
- it 'should destroy the contained objects when the container is destroyed' do
58
- vc = @plugin_with_db_model.visit_count
59
- i0 = @plugin_with_db_model.items[0]
60
- i1 = @plugin_with_db_model.items[1]
61
-
62
- DBModel.all.for_ids(vc.id, i0.id, i1.id).count.should == 3
63
-
64
- @plugin_with_db_model.db_model_container.destroy
65
-
66
- DBModel.all.for_ids(vc.id, i0.id, i1.id).count.should == 0
67
- end
68
-
69
- it 'should allow relationships between DBModels' do
70
- plugin = PluginWithDBModelRelationships.new({})
71
-
72
- t1 = plugin.teachers.build(name: 'Mr. Adams')
73
- t2 = plugin.teachers.build(name: 'Ms. Boudreau')
74
-
75
- s1 = plugin.students.build(name: 'Alex', teacher: t1)
76
- s2 = plugin.students.build(name: 'Billy', teacher: t1)
77
- s3 = plugin.students.build(name: 'Caitlyn', teacher: t2)
78
-
79
- plugin.save_db_model_container.should be_true
80
-
81
- # Reload from database
82
- plugin = PluginWithDBModelRelationships.new({})
83
-
84
- # Check all the names and relationships to make sure they were
85
- # persisted
86
- teachers = plugin.teachers.to_a
87
- students = plugin.students.to_a
88
-
89
- teachers[0].name.should == 'Mr. Adams'
90
- teachers[0].students.count.should == 2
91
- teachers[0].students.should include(s1)
92
- teachers[0].students.should include(s2)
93
-
94
- teachers[1].name.should == 'Ms. Boudreau'
95
- teachers[1].students.count.should == 1
96
- teachers[1].students.should include(s3)
97
-
98
- s1.name.should == 'Alex'
99
- s1.teacher.should == t1
100
- s2.name.should == 'Billy'
101
- s2.teacher.should == t1
102
- s3.name.should == 'Caitlyn'
103
- s3.teacher.should == t2
104
- end
105
-
106
- context 'with multiple containers' do
107
-
108
- it 'should save the data in the correct container' do
109
- plugin = PluginWithDBModel.new({})
110
-
111
- plugin.with_db_model_container('my_container') do
112
- plugin.build_visit_count(count: 10)
113
- end
114
-
115
- plugin.use_db_model_container('my_other_container')
116
- plugin.build_visit_count(count: 20)
117
- plugin.save_db_model_container.should be_true
118
- plugin.reset_current_db_model_container
119
-
120
- plugin.use_db_model_container('my_container')
121
- plugin.db_model_container.changed?.should be_true
122
- plugin.save_db_model_container.should be_true
123
- plugin.db_model_container.changed?.should be_false
124
-
125
- # Reload from the database
126
- @plugin_with_db_model = PluginWithDBModel.new({})
127
-
128
- # Make sure everything is in the right container and make sure we can
129
- # access the containers how we want
130
- @plugin_with_db_model.with_db_model_container('my_other_container') do
131
- @plugin_with_db_model.current_db_model_container_name.should ==
132
- 'my_other_container'
133
- @plugin_with_db_model.visit_count.count.should == 20
134
- end
135
-
136
- @plugin_with_db_model.use_db_model_container('my_container')
137
- @plugin_with_db_model.current_db_model_container_name.should ==
138
- 'my_container'
139
- @plugin_with_db_model.visit_count.count.should == 10
140
-
141
- @plugin_with_db_model.reset_current_db_model_container
142
- @plugin_with_db_model.current_db_model_container_name.should be_nil
143
- @plugin_with_db_model.visit_count.count.should == 5
144
- end
145
-
146
- it 'should revert the current container name after using with_db_model_container' do
147
- @plugin_with_db_model.use_db_model_container('fake_container')
148
- @plugin_with_db_model.current_db_model_container_name.should ==
149
- 'fake_container'
150
-
151
- @plugin_with_db_model.with_db_model_container('my_container') do
152
- @plugin_with_db_model.current_db_model_container_name.should ==
153
- 'my_container'
154
- end
155
-
156
- @plugin_with_db_model.current_db_model_container_name.should ==
157
- 'fake_container'
158
- end
159
-
160
- it 'should only load each container once' do
161
- container_names = %w{my_container my_other_container}
162
-
163
- # The default container has already been loaded
164
- @plugin_with_db_model.expects(:load_db_model_container).with(nil).never
165
-
166
- # The others should only be called once. Just return something
167
- # non-nil
168
- container_names.each do |name|
169
- @plugin_with_db_model.expects(:load_db_model_container).with(
170
- name).returns(1).once
171
- end
172
-
173
- 3.times do
174
- container_names.each do |name|
175
- @plugin_with_db_model.with_db_model_container(name) do
176
- # Access the container
177
- @plugin_with_db_model.db_model_container
178
- end
179
- end
180
- end
181
- end
182
-
183
- it 'should not share containers between plugin objects' do
184
- p0 = PluginWithDBModel.new({})
185
- p1 = PluginWithDBModelRelationships.new({})
186
-
187
- # Default container
188
- p0.save_db_model_container.should be_true
189
- p1.save_db_model_container.should be_true
190
- p0.db_model_container.should_not == p1.db_model_container
191
-
192
- # Named container
193
- p0.with_db_model_container('my_container') do
194
- p1.with_db_model_container('my_container') do
195
- p0.save_db_model_container.should be_true
196
- p1.save_db_model_container.should be_true
197
- p0.db_model_container.should_not == p1.db_model_container
198
- end
199
- end
200
- end
201
-
202
- end
203
-
204
- end
205
- end
206
- end