lolita 3.1.1 → 3.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -7,9 +7,9 @@ gem "abstract"
7
7
  gem "builder", "~> 2.1.2" #cucumber asks for builder 3 but rails supports 2.1
8
8
 
9
9
  group :mongoid do
10
- gem "mongo"
11
- gem "mongoid", "~> 2.0.0.rc.7"
12
- gem "bson_ext"
10
+ gem "mongo", "~> 1.3.0"
11
+ gem "mongoid", "~> 2.0.0"
12
+ gem "bson_ext", "~> 1.3.0"
13
13
  end
14
14
 
15
15
  #gem 'cover_me', '>= 1.0.0.rc6', :group => :test
data/History.rdoc CHANGED
@@ -1,3 +1,21 @@
1
+ === Version 3.1.2 / 2011-04-14
2
+ * Enhancements
3
+ * Hooks for Lolita::RestController addeed (Arturs Meisters)
4
+ * Form save response splited in different methods for easy way to override (Arturs Meisters)
5
+ * Hooks for #render_components (Arturs Meisters)
6
+ * Hook #run return results from methods and blocks as one big string (Arturs Meisters)
7
+ * Hooks :run_scope inventend (Arturs Meisters)
8
+
9
+ * Bug fixes
10
+ * Migration detection fixed (Arturs Meisters)
11
+ * Hooks named fire methods fixed (Arturs Meisters)
12
+ * Bug fixes in hooks (Arturs Meisters)
13
+ * Builder bug fix, in production (Arturs Meisters)
14
+
15
+ === Version 3.1.1 / 2011-04-13
16
+ * Enhancements
17
+ * Filters for list added (Gatis Tomsons)
18
+
1
19
  === Version 3.1.0 / 2011-04-12
2
20
  * Enhancements
3
21
  * Hooks added (Arturs Meisters)
@@ -66,5 +84,5 @@
66
84
  * New look (Rolands Bondars)
67
85
 
68
86
  === Versions before 3.0.3
69
- Lolita 3.0 is completely different from previous versions and don't have any compability or architecture same as
87
+ Lolita 3 is completely different from previous versions and don't have any compability or architecture same as
70
88
  old versions.
data/README.rdoc CHANGED
@@ -48,7 +48,49 @@ to all controllers.
48
48
  If you decide to use some newer version, you most likely should copy new assets to your project.
49
49
  This can be done with
50
50
  rails g lolita:assets
51
+ ===Using hooks
51
52
 
53
+ Lolita define hooks for RestController and for components.
54
+ ====RestController hooks
55
+
56
+ There are two kind of hooks for all actions - <em>before_[action name]</em> and <em>after_[action name]</em>.
57
+ Define callbacks for those hooks outside of controller. This will call User#log_action each time when #destroy
58
+ action is requested.
59
+ Lolita::RestController.before_destroy do
60
+ User.log_action("Going to delete #{params[:id]}")
61
+ end
62
+ Also you can define callbacks in your controllers that extend Lolita::RestController. This will call #set_default_params
63
+ each time #new action is requested.
64
+ class PostController < Lolita::RestController
65
+ before_new :set_default_params
66
+
67
+ private
68
+
69
+ def set_default_params
70
+ params[:post][:title]="-Your title goes here-"
71
+ end
72
+ end
73
+ ====Component hooks
74
+
75
+ Components have three hooks - <em>before</em>, <em>after</em> and <em>around</em>.
76
+ Component hooks are different from controller hooks with names. Each component has it's own name, that is used to
77
+ call component, like
78
+ render_component :"lolita/configuration/list/display"
79
+ #same as
80
+ render_component :"lolita/configuration/list", :display
81
+ and this name is used to add callback for component. As components is not related to specific class, then there
82
+ are only one way to define callback for them.
83
+ Lolita::Hooks.component(:"lolita/configuration/list/display").before do
84
+ "<div>My Custom text</div>"
85
+ end
86
+ That what are inside of blocks depends on place where you define callback if it is in <i>.rb</i> file, than you
87
+ should put HTML in quotes, if in <i>.erb</i> and similar files then there is no need for that. Also blocks with
88
+ Ruby code only return last line, so you should probably define HTML as shown in previous example.
89
+ For <b>around</b> callback if you pass block, then original content will be replaced with that, but if you want
90
+ to let original content inside of your block content than it is done like this with #let_content method.
91
+ Lolita::Hooks.component(:"lolita/configuration/list/display").around do
92
+ "<div style='color:red'>#{let_content}</div>"
93
+ end
52
94
  ==License
53
95
 
54
96
  Lolita is under MIT license. See LICENSE.txt[https://github.com/ithouse/lolita/blob/master/LICENSE.txt]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.1.1
1
+ 3.1.2
@@ -1,44 +1,57 @@
1
1
  class Lolita::RestController < ApplicationController
2
2
  include Lolita::Controllers::UserHelpers
3
3
  include Lolita::Controllers::InternalHelpers
4
+
5
+ include Lolita::Hooks
6
+ add_hook :before_new, :after_new, :before_create,:after_create,:before_edit,:after_edit
7
+ add_hook :before_update,:after_update,:before_destroy,:after_destroy,:before_index,:after_index
8
+ add_hook :before_build_resource, :after_build_resource
4
9
 
5
10
  before_filter :authenticate_lolita_user!
6
11
  layout "lolita/application"
7
12
 
8
13
  def new
14
+ self.run(:before_new)
9
15
  build_resource
10
16
  show_form
11
17
  end
12
18
 
13
19
  def create
20
+ self.run(:before_create)
14
21
  build_resource
15
22
  save_and_redirect
16
23
  end
17
24
 
18
25
  def edit
26
+ self.run(:before_edit)
19
27
  get_resource
20
28
  show_form
21
29
  end
22
30
 
23
31
  def update
32
+ self.run(:before_update)
24
33
  get_resource
25
34
  if self.resource
26
35
  self.resource=resource_with_attributes(self.resource,resource_attributes)
27
36
  save_and_redirect
37
+
28
38
  end
29
39
  end
30
40
 
31
41
  def destroy
42
+ self.run(:before_destroy)
32
43
  get_resource
33
44
  if self.resource && self.resource.destroy
34
45
  flash.now[:notice] = I18n.t "lolita.shared.destroy_notice"
35
46
  else
36
47
  flash.now[:alert] = I18n.t "lolita.shared.destroy_alert"
37
48
  end
49
+ self.run(:after_destroy)
38
50
  redirect_to :action=>"index"
39
51
  end
40
52
 
41
53
  def index
54
+ self.run(:before_index)
42
55
  respond_to do |format|
43
56
  format.html do
44
57
  build_response_for(:list,:page=>page)
@@ -47,40 +60,59 @@ class Lolita::RestController < ApplicationController
47
60
  render :json=>page
48
61
  end
49
62
  end
63
+ self.run(:after_index)
50
64
  end
51
65
 
52
66
  private
53
67
 
54
68
  def show_form
55
- #TODO Valdis: ja es extendoju rest_controller un gribu pārdefinēt edit actionu, man vajag lai
56
- # varu norādīt citu šablonu, piemēram, manā gadījumā ir services_controller un jamais meklē
57
- # "services/form", bet es gribu izmantot to pašu "lolita/rest/form"
58
- # tagad man viss šitais jāpārkopē uz projektu
59
-
60
69
  build_response_for(:tabs)
70
+ self.run(:"after_#{params[:action]}")
61
71
  if request.xhr?
62
- render :form, :layout => false
72
+ render "/lolita/rest/form", :layout => false
63
73
  else
64
- render :form
74
+ render "/lolita/rest/form"
65
75
  end
66
76
  end
67
77
 
68
78
  def save_and_redirect
69
- respond_to do |format|
70
- format.html do
71
- if self.resource.save
72
- flash.now[:notice] = I18n.t "lolita.shared.save_notice"
73
- else
74
- flash.now[:alert] = I18n.t "lolita.shared.save_alert"
75
- end
76
- show_form
77
- end
78
- format.json do
79
- render :status => self.resource.save ? 200 : 400, :json => self.resource
80
- end
79
+ respond_to do |format|
80
+ if self.resource.save
81
+ self.resource.reload
82
+ format.html{ respond_html_200}
83
+ format.json{ respond_json_200}
84
+ else
85
+ format.html{ respond_html_400 }
86
+ format.json{ respond_json_400 }
81
87
  end
88
+ end
82
89
  end
83
90
 
91
+ def respond_html_200
92
+ response.headers["Validation"] = 'true'
93
+ flash.now[:notice] = I18n.t "lolita.shared.save_notice"
94
+ show_form
95
+ end
96
+
97
+ def respond_html_400
98
+ response.headers["Validation"] = 'true'
99
+ flash.now[:alert] = I18n.t "lolita.shared.save_alert"
100
+ show_form
101
+ end
102
+
103
+ def respond_json_200
104
+ respond_json(200)
105
+ end
106
+
107
+ def respond_json_400
108
+ respond_json(400)
109
+ end
110
+
111
+ def respond_json(status)
112
+ self.run(:"after_#{params[:action]}")
113
+ render :status=>status, :json=>self.resource
114
+ end
115
+
84
116
  def to_list
85
117
  builder=build_response_for(:list,:page=>page)
86
118
  render :index
data/lib/lolita.rb CHANGED
@@ -4,16 +4,16 @@ LOLITA_VERSION=File.read(File.expand_path("../../VERSION",__FILE__)).gsub(/[^.\w
4
4
  puts "=> Lolita #{LOLITA_VERSION} starting#{defined?(Rails) ? " with Rails" : ""}"
5
5
 
6
6
  # TODO should allow run lolita seperated
7
- unless (["-d","--debug"] & ARGV).empty?
8
- require "ruby-debug"
9
- Debugger.settings[:autoeval]=true
10
- else
11
- unless self.respond_to?(:debugger)
12
- def debugger
13
- warn "Debugger called at #{caller.first} was ignored, run lolita with -d to attatch debugger."
14
- end
15
- end
16
- end
7
+ # unless (["-d","--debug"] & ARGV).empty?
8
+ # require "ruby-debug"
9
+ # Debugger.settings[:autoeval]=true
10
+ # else
11
+ # unless self.respond_to?(:debugger)
12
+ # def debugger
13
+ # warn "Debugger called at #{caller.first} was ignored, run lolita with -d to attatch debugger."
14
+ # end
15
+ # end
16
+ # end
17
17
 
18
18
  require 'abstract'
19
19
  require 'active_support/core_ext/numeric/time'
@@ -34,14 +34,15 @@ module Lolita
34
34
  # default state is <code>:display</code>
35
35
  def get_builder(*value)
36
36
  if value && !value.empty?
37
- set_builder(value)
37
+ builder_values_from(value)
38
38
  elsif @builder
39
- set_builder(@builder)
39
+ builder_values_from(@builder)
40
40
  else
41
41
  unless @builder
42
- @builder=default_builder
42
+ default_builder
43
+ else
44
+ @builder
43
45
  end
44
- @builder
45
46
  end
46
47
  end
47
48
 
@@ -52,13 +53,13 @@ module Lolita
52
53
 
53
54
  private
54
55
 
55
- def set_builder value
56
+ def builder_values_from value
56
57
  if value.is_a?(Hash)
57
- @builder=value
58
+ value
58
59
  elsif value.is_a?(Array)
59
- @builder={:name=>fix_name(value[0]),:state=>value[1] || default_build_state}
60
+ {:name=>fix_name(value[0]),:state=>value[1] || default_build_state}
60
61
  else
61
- @builder={:name=>fix_name(value),:state=>default_build_state}
62
+ {:name=>fix_name(value),:state=>default_build_state}
62
63
  end
63
64
  end
64
65
 
@@ -26,23 +26,41 @@ module Lolita
26
26
  # render_component "lolita/list", :display
27
27
  # render_component "lolita/list/display"
28
28
  def render_component *args
29
-
30
29
  @opts=args.extract_options!
31
30
  name=args[0]
32
31
  state=args[1]
33
32
  format=@opts.delete(:format)
33
+
34
34
  raise "Can't render component without name!" unless name
35
35
  will_use_component name
36
- partial_name=File.join("/components",name.to_s,state ? state.to_s : nil)
37
- if format
38
- with_format(format) do
39
- render(:partial=>partial_name,:locals=>@opts)
36
+ component_name=File.join(name.to_s,state ? state.to_s : nil)
37
+ partial_name=File.join("/components",component_name)
38
+ output=output_component(partial_name,component_name,:format=>format,:locals=>@opts)
39
+ self.respond_to?(:raw) ? raw(output) : output
40
+ end
41
+
42
+ def output_component(partial_name,name,options={})
43
+ output=""
44
+ if options[:format]
45
+ with_format(options[:format]) do
46
+ output << output_with_callbacks(partial_name,name,options[:locals])
40
47
  end
41
48
  else
42
- render(:partial=>partial_name,:locals=>@opts)
49
+ output << output_with_callbacks(partial_name,name,options[:locals])
43
50
  end
51
+ output
44
52
  end
45
-
53
+
54
+ def output_with_callbacks(partial_name,name,locals)
55
+ output= Lolita::Hooks.component(name).run(:before,:run_scope=>self).to_s
56
+ block_output=Lolita::Hooks.component(name).run(:around, :run_scope=>self) do
57
+ render(:partial=>partial_name,:locals=>locals)
58
+ end
59
+ output << block_output.to_s
60
+ output << Lolita::Hooks.component(name).run(:after,:run_scope=>self).to_s
61
+ output
62
+ end
63
+
46
64
  def with_format(format, &block)
47
65
  old_formats = formats
48
66
  self.formats = [format]
@@ -60,8 +60,10 @@ module Lolita
60
60
  end
61
61
 
62
62
  def build_resource(attributes=nil)
63
+ self.run(:before_build_resource)
63
64
  attributes||=resource_attributes
64
65
  self.resource=resource_with_attributes(resource_class.new,attributes)
66
+ self.run(:after_build_resource)
65
67
  end
66
68
 
67
69
  def build_response_for(conf_part,options={})
data/lib/lolita/hooks.rb CHANGED
@@ -92,6 +92,7 @@ module Lolita
92
92
  end
93
93
 
94
94
  module ClassMethods
95
+ attr_accessor :hooks_run_scope
95
96
 
96
97
  # Setter for #hook_scope.
97
98
  def hooks_scope=(object)
@@ -154,14 +155,17 @@ module Lolita
154
155
  # MyClass.run(:before_save,:after_save,:scope=>MyClass.new)
155
156
  # # this will call callbacks in MyClass instance scope, that means that self will be MyClass instance.
156
157
  def run(*hook_names,&block)
158
+
159
+ result=nil
157
160
  options=hook_names.extract_options!
158
161
  (hook_names || []).each do |hook_name|
159
162
  raise Lolita::HookNotFound, "Hook #{hook_name} is not defined for #{self}." unless self.has_hook?(hook_name)
160
- in_hooks_scope(options[:scope]) do
163
+ in_hooks_scope(options[:scope],options[:run_scope]) do
161
164
  callback=get_callback(hook_name)
162
- run_callback(callback,&block)
165
+ result=run_callback(callback,&block)
163
166
  end
164
167
  end
168
+ result
165
169
  end
166
170
 
167
171
  # Is hook with <em>name</em> is defined for class.
@@ -192,7 +196,7 @@ module Lolita
192
196
  # Set #method_missing
193
197
  def recognize_hook_methods method_name, *args, &block
194
198
  if method_name.to_s.match(/^run_(\w+)/)
195
- self.run($1,&block)
199
+ self.run($1,*args,&block)
196
200
  true
197
201
  end
198
202
  end
@@ -201,46 +205,61 @@ module Lolita
201
205
 
202
206
  # Switch between self and given <em>scope</em>. Block will be executed with <em>scope</em>.
203
207
  # And after that it will switch back to self.
204
- def in_hooks_scope(scope)
208
+ def in_hooks_scope(scope,run_scope=nil)
205
209
  begin
206
210
  self.hooks_scope=scope||self
211
+ if run_scope
212
+ run_scope.define_singleton_method(:let_content) do
213
+ scope.let_content
214
+ end
215
+ end
216
+ self.hooks_run_scope=run_scope || self.hooks_scope
207
217
  yield
208
218
  ensure
209
219
  self.hooks_scope=self
220
+ self.hooks_run_scope=self.hooks_scope
210
221
  end
211
222
  end
212
223
 
213
224
  # Run callback. Each callback is Hash with <i>:methods</i> Array and </i>:blocks</i> Array
214
225
  def run_callback(callback,&block)
215
- run_methods(callback[:methods],&block)
216
- run_blocks(callback[:blocks],&block)
226
+ method_results=run_methods(callback[:methods],&block)
227
+ block_results=run_blocks(callback[:blocks],&block)
228
+ method_results+block_results
217
229
  end
218
230
 
219
231
  # Run methods from <em>methods</em> Array
220
232
  def run_methods methods, &block
233
+ result=""
221
234
  (methods||[]).each do |method_name|
222
- hooks_scope.__send__(method_name,&block)
235
+ result << (hooks_run_scope.__send__(method_name,&block)).to_s
223
236
  end
237
+ result
224
238
  end
225
239
 
226
240
  # Run blocks from <em>blocks</em> Array. Also it set #given_callback_content if block is given, this
227
241
  # will allow to call #let_content. Each block is runned with #run_block.
228
242
  def run_blocks blocks,&given_block
243
+ result=""
229
244
  (blocks||[]).each do |block|
230
245
  begin
231
246
  if block_given?
232
247
  self.given_callback_content=given_block
233
248
  end
234
- run_block(block,&given_block)
249
+ result << (run_block(block,&given_block)).to_s
235
250
  ensure
236
251
  self.given_callback_content=nil
237
252
  end
238
253
  end
254
+ if block_given? && (!blocks || (blocks && blocks.empty?))
255
+ result << run_block(given_block).to_s
256
+ end
257
+ result
239
258
  end
240
259
 
241
260
  # Run block in scope.
242
261
  def run_block block, &given_block
243
- hooks_scope.instance_eval(&block)
262
+ hooks_run_scope.instance_eval(&block)
244
263
  end
245
264
 
246
265
  # Return all callbacks
@@ -277,8 +296,10 @@ module Lolita
277
296
  module InstanceMethods
278
297
 
279
298
  # See Lolita::Hooks::ClassMethods#run
280
- def run(*hook_names)
281
- self.class.run(*hook_names,:scope=>self)
299
+ def run(*hook_names,&block)
300
+ options=hook_names ? hook_names.extract_options! : {}
301
+ options[:scope]=self
302
+ self.class.run(*hook_names,options,&block)
282
303
  end
283
304
 
284
305
  # See Lolita::Hooks::ClassMethods#let_content
@@ -288,7 +309,7 @@ module Lolita
288
309
 
289
310
  # See Lolita::Hooks::ClassMethods#method_missing
290
311
  def method_missing(*args,&block)
291
- unless self.class.recognize_hook_methods(*args,&block)
312
+ unless self.class.recognize_hook_methods(*args,:scope=>self,&block)
292
313
  super
293
314
  end
294
315
  end
@@ -1,3 +1,8 @@
1
1
  require 'lolita/mapping'
2
2
  require 'lolita/rails'
3
- require 'lolita/modules'
3
+ require 'lolita/modules'
4
+
5
+ Lolita::Hooks::NamedHook.add(:components)
6
+ Lolita::Hooks.components.class_eval{
7
+ add_hook :before,:after,:around
8
+ }
@@ -110,7 +110,7 @@ class RouteSet
110
110
  private
111
111
 
112
112
  def migrating?
113
- File.basename($0)=="rake" && $ARGV.include?("db:migrate")
113
+ File.basename($0)=="rake" && (ARGV.include?("db:migrate"))
114
114
  end
115
115
  end
116
116
  end
data/lolita.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{lolita}
8
- s.version = "3.1.1"
8
+ s.version = "3.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["ITHouse (Latvia) and Arturs Meisters"]
12
- s.date = %q{2011-04-13}
12
+ s.date = %q{2011-04-14}
13
13
  s.description = %q{Great Rails CMS, that turns your business logic into good-looking, fully functional workspace. }
14
14
  s.email = %q{support@ithouse.lv}
15
15
  s.extra_rdoc_files = [
@@ -212,11 +212,13 @@ Gem::Specification.new do |s|
212
212
  "spec/configuration/tab_spec.rb",
213
213
  "spec/configuration/tabs_spec.rb",
214
214
  "spec/controllers/internal_helpers_spec.rb",
215
+ "spec/controllers/lolita_rest_nested_resources_spec.rb",
215
216
  "spec/controllers/lolita_rest_spec.rb",
216
217
  "spec/dbi/base_spec.rb",
217
218
  "spec/hooks_spec.rb",
218
219
  "spec/lolita_spec.rb",
219
220
  "spec/mapping_spec.rb",
221
+ "spec/navigation/branch_spec.rb",
220
222
  "spec/navigation/tree_spec.rb",
221
223
  "spec/orm/mongoid.rb",
222
224
  "spec/rails_app/app/controllers/application_controller.rb",
@@ -326,11 +328,13 @@ Gem::Specification.new do |s|
326
328
  "spec/configuration/tab_spec.rb",
327
329
  "spec/configuration/tabs_spec.rb",
328
330
  "spec/controllers/internal_helpers_spec.rb",
331
+ "spec/controllers/lolita_rest_nested_resources_spec.rb",
329
332
  "spec/controllers/lolita_rest_spec.rb",
330
333
  "spec/dbi/base_spec.rb",
331
334
  "spec/hooks_spec.rb",
332
335
  "spec/lolita_spec.rb",
333
336
  "spec/mapping_spec.rb",
337
+ "spec/navigation/branch_spec.rb",
334
338
  "spec/navigation/tree_spec.rb",
335
339
  "spec/orm/mongoid.rb",
336
340
  "spec/rails_app/app/controllers/application_controller.rb",
@@ -5,6 +5,21 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
5
5
 
6
6
  class MyController < ApplicationController
7
7
  include Lolita::Controllers::InternalHelpers
8
+ include Lolita::Hooks
9
+ add_hook :before_build_resource,:after_build_resource
10
+ add_hook :before_index
11
+ before_index :modify
12
+
13
+ def index
14
+ @@temp=1
15
+ self.run_before_index
16
+ end
17
+
18
+ private
19
+
20
+ def modify
21
+ @@temp=3
22
+ end
8
23
  end
9
24
 
10
25
  describe MyController do
@@ -12,6 +27,11 @@ describe MyController do
12
27
  @controller.request.env["lolita.mapping"]=Lolita.mappings[:post]
13
28
  end
14
29
 
30
+ it "should call hook in #index" do
31
+ @controller.index
32
+ @controller.class.class_variable_get(:"@@temp").should == 3
33
+ end
34
+
15
35
  it "should get resource name" do
16
36
  @controller.resource_name.should == :post
17
37
  end
@@ -0,0 +1,33 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Lolita::RestController do
4
+ render_views
5
+
6
+ before(:each) do
7
+ @controller.request.env["lolita.mapping"]=Lolita.mappings[:post]
8
+ end
9
+
10
+ it "should create new nested resources together with base resource"
11
+ # do
12
+ # p = {:title=>"Post 1"}
13
+ # p[:comments_attributes] = [{:body=>"Comment 1"}, {:body=>"Comment 2"}]
14
+ # post :create, :post=>p
15
+ # Post.last.title.should == "Post 1"
16
+ # puts Comment.all.to_a.inspect
17
+ # Post.first.comments.length.should == 2
18
+ # Post.first.comments.last.body.should == "Comment 2"
19
+ # end
20
+
21
+ it "should create new nested resources for existing base resource"
22
+
23
+ it "should add new nested resources for existing base resource with existing nested resourcer"
24
+
25
+ it "should remove nested resources from base resource"
26
+
27
+ it "should modify nested resources for given base resource"
28
+
29
+
30
+ end
31
+
32
+
33
+
@@ -7,6 +7,17 @@ describe Lolita::RestController do
7
7
  @controller.request.env["lolita.mapping"]=Lolita.mappings[:post]
8
8
  end
9
9
 
10
+ describe "hooks" do
11
+ it "should call hooks on #index action" do
12
+ @controller.class.class_variable_set(:"@@temp",1)
13
+ @controller.before_index do
14
+ @@temp=2
15
+ end
16
+ get :index
17
+ @controller.class.class_variable_get(:"@@temp").should == 2
18
+ end
19
+ end
20
+
10
21
  it "should render list component for index action" do
11
22
  get :index
12
23
  response.should render_template("index")
data/spec/hooks_spec.rb CHANGED
@@ -26,6 +26,7 @@ class Counter
26
26
  @value
27
27
  end
28
28
  end
29
+
29
30
  describe Lolita::Hooks do
30
31
  after(:each) do
31
32
  MyClass.clear_hooks if MyClass.respond_to?(:clear_hooks)
@@ -116,20 +117,75 @@ describe Lolita::Hooks do
116
117
  end
117
118
 
118
119
 
120
+ describe "block content" do
121
+ it "should return block content" do
122
+ MyClass.after_load do
123
+ "My name"+
124
+ let_content.to_s
125
+ end
126
+
127
+ output=MyClass.run(:after_load) do
128
+ " is Earl"
129
+ end
130
+
131
+ output.should == "My name is Earl"
132
+ end
133
+ end
134
+
119
135
  context "wrap around" do
120
136
 
121
137
  it "should allow to wrap around when #run receive block" do
122
138
  MyClass.after_load do
123
139
  value("first")
124
140
  let_content()
125
- value("second")
126
141
  end
127
142
 
128
143
  MyClass.run(:after_load) do
129
- value().should=="first"
144
+ value("second")
130
145
  end
131
146
  MyClass.value.should == "second"
132
147
  end
148
+
149
+ it "should call default block when no block passed as callback" do
150
+ MyClass.run(:after_load) do
151
+ value("inblock")
152
+ end
153
+
154
+ MyClass.value.should=="inblock"
155
+ end
156
+ end
157
+
158
+ context "methods as callbacks" do
159
+ class MethodTestClass
160
+ include Lolita::Hooks
161
+ add_hook :before_save
162
+ before_save :set_name
163
+
164
+ def save
165
+ self.run_before_save
166
+ end
167
+
168
+ private
169
+
170
+ def self.set_name
171
+ @name="Class name"
172
+ end
173
+
174
+ def set_name
175
+ @name="Name"
176
+ end
177
+ end
178
+
179
+ it "should call callback for method on class" do
180
+ MethodTestClass.run_before_save
181
+ MethodTestClass.instance_variable_get(:"@name").should == "Class name"
182
+ end
183
+
184
+ it "should run callbac on instance" do
185
+ object=MethodTestClass.new
186
+ object.save
187
+ object.instance_variable_get(:"@name").should == "Name"
188
+ end
133
189
  end
134
190
  end
135
191
 
@@ -174,6 +230,15 @@ describe Lolita::Hooks do
174
230
  Counter.get.should == 2
175
231
  end
176
232
 
233
+ it "should execute run block when no callback block given" do
234
+ Lolita::Hooks.components.add_hook(:around)
235
+ Counter.set(0)
236
+ Lolita::Hooks.components.run(:around) do
237
+ Counter.set(1)
238
+ end
239
+ Counter.get.should==1
240
+ end
241
+
177
242
  it "should run all named hook callbacks when runned on named collection" do
178
243
  pending "Need to update functionality to work."
179
244
  Lolita::Hooks.components.add_hook(:after)
@@ -0,0 +1,80 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../simple_spec_helper')
2
+
3
+ describe Lolita::Navigation::Branch do
4
+ let(:tree){Lolita::Navigation::Tree.new("test tree")}
5
+
6
+ describe "attributes" do
7
+ let(:branch){Lolita::Navigation::Branch.new}
8
+
9
+ it "should have title" do
10
+ branch.title="Pages"
11
+ branch.title.should == "Pages"
12
+ end
13
+
14
+ it "should have default name" do
15
+ branch.name.should match(/branch/)
16
+ end
17
+
18
+ it "should not have level when branch is not in tree" do
19
+ branch.level.should be_nil
20
+ end
21
+
22
+ it "should have options, that may containe any values needed" do
23
+ branch.options[:url]="/"
24
+ branch.options[:url].should == "/"
25
+ end
26
+
27
+ it "should belongs to tree" do
28
+ branch.tree.should be_nil
29
+ lambda{
30
+ branch.tree=Object.new
31
+ }.should raise_error(ArgumentError)
32
+ end
33
+
34
+ context "relationships" do
35
+
36
+ it "should have parent" do
37
+ branch=tree.add(Object)
38
+ branch.parent.should == tree.root
39
+ end
40
+
41
+ it "should have childern" do
42
+ branch.append(Lolita::Navigation::Branch.new)
43
+ branch.children.class.should == Lolita::Navigation::Tree
44
+ branch.children.should have(1).item
45
+ end
46
+
47
+ it "should have siblings" do
48
+ tree.add(Object)
49
+ branch=tree.add(String)
50
+ branch.siblings[:before].should_not be_nil
51
+ branch.siblings[:after].should be_nil
52
+ end
53
+ end
54
+ end
55
+
56
+ describe "adding" do
57
+ let(:new_branch){Lolita::Navigation::Branch.new}
58
+
59
+ it "should append to existing branch" do
60
+ new_branch.append(Lolita::Navigation::Branch.new)
61
+ new_branch.append(Object)
62
+ new_branch.append("Pages")
63
+ new_branch.children.should have(3).items
64
+ end
65
+
66
+ it "should prepend to exsiting branch" do
67
+ new_branch.append(Object)
68
+ new_branch.prepend(String)
69
+ new_branch.children.first.object.should == String
70
+ end
71
+
72
+ it "should add before and after branch" do
73
+ branch=tree.add(Object)
74
+ branch.before(Lolita::Navigation::Branch.new)
75
+ branch.after(Object)
76
+ branch.siblings.values.compact.should have(2).items
77
+ end
78
+ end
79
+
80
+ end
@@ -27,7 +27,7 @@ describe Lolita::Navigation::Tree do
27
27
 
28
28
  context "#add" do
29
29
  it "should accept object, position and options" do
30
- tree.add(Object,:append,:url=>"/mypath")
30
+ tree.add(Object.new,:append,:url=>"/mypath")
31
31
  tree.should have(1).branch
32
32
  end
33
33
  end
@@ -38,7 +38,7 @@ describe Lolita::Navigation::Tree do
38
38
 
39
39
  it "should iterate through all branches" do
40
40
  0.upto(3){|i|
41
- tree.add(Object,:append,:url=>"/#{i}",:name=>"branch#{i}")
41
+ tree.add(Object.new,:append,:url=>"/#{i}",:name=>"branch#{i}")
42
42
  }
43
43
  tree.each_with_index do |branch,index|
44
44
  branch.name.should == "branch#{index}"
@@ -12,5 +12,8 @@ class Post
12
12
  has_many :comments
13
13
  belongs_to :profile
14
14
  validates_presence_of :title
15
+
16
+ accepts_nested_attributes_for :comments, :reject_if => :all_blank
17
+
15
18
  lolita
16
19
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: lolita
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 3.1.1
5
+ version: 3.1.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - ITHouse (Latvia) and Arturs Meisters
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-13 00:00:00 +03:00
13
+ date: 2011-04-14 00:00:00 +03:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -372,11 +372,13 @@ files:
372
372
  - spec/configuration/tab_spec.rb
373
373
  - spec/configuration/tabs_spec.rb
374
374
  - spec/controllers/internal_helpers_spec.rb
375
+ - spec/controllers/lolita_rest_nested_resources_spec.rb
375
376
  - spec/controllers/lolita_rest_spec.rb
376
377
  - spec/dbi/base_spec.rb
377
378
  - spec/hooks_spec.rb
378
379
  - spec/lolita_spec.rb
379
380
  - spec/mapping_spec.rb
381
+ - spec/navigation/branch_spec.rb
380
382
  - spec/navigation/tree_spec.rb
381
383
  - spec/orm/mongoid.rb
382
384
  - spec/rails_app/app/controllers/application_controller.rb
@@ -480,7 +482,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
480
482
  requirements:
481
483
  - - ">="
482
484
  - !ruby/object:Gem::Version
483
- hash: -453610993
485
+ hash: 43629427
484
486
  segments:
485
487
  - 0
486
488
  version: "0"
@@ -511,11 +513,13 @@ test_files:
511
513
  - spec/configuration/tab_spec.rb
512
514
  - spec/configuration/tabs_spec.rb
513
515
  - spec/controllers/internal_helpers_spec.rb
516
+ - spec/controllers/lolita_rest_nested_resources_spec.rb
514
517
  - spec/controllers/lolita_rest_spec.rb
515
518
  - spec/dbi/base_spec.rb
516
519
  - spec/hooks_spec.rb
517
520
  - spec/lolita_spec.rb
518
521
  - spec/mapping_spec.rb
522
+ - spec/navigation/branch_spec.rb
519
523
  - spec/navigation/tree_spec.rb
520
524
  - spec/orm/mongoid.rb
521
525
  - spec/rails_app/app/controllers/application_controller.rb