lolita 3.1.2 → 3.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/History.rdoc CHANGED
@@ -1,3 +1,11 @@
1
+ === Version 3.1.3 /
2
+ * Enhancements
3
+ * Lolita::Navigation::Tree and Lolita::Navigation::Branch added (Arturs Meisters)
4
+ * Main navigation tree generate from resources (Arturs Meisters)
5
+
6
+ * Bug fixes
7
+ * Hooks #let_content fixed to change after first block. (Arturs Meisters)
8
+
1
9
  === Version 3.1.2 / 2011-04-14
2
10
  * Enhancements
3
11
  * Hooks for Lolita::RestController addeed (Arturs Meisters)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.1.2
1
+ 3.1.3
@@ -47,7 +47,7 @@ class Lolita::RestController < ApplicationController
47
47
  flash.now[:alert] = I18n.t "lolita.shared.destroy_alert"
48
48
  end
49
49
  self.run(:after_destroy)
50
- redirect_to :action=>"index"
50
+ redirect_to lolita_resources_path
51
51
  end
52
52
 
53
53
  def index
@@ -95,7 +95,7 @@ class Lolita::RestController < ApplicationController
95
95
  end
96
96
 
97
97
  def respond_html_400
98
- response.headers["Validation"] = 'true'
98
+ response.headers["Validation"] = 'false'
99
99
  flash.now[:alert] = I18n.t "lolita.shared.save_alert"
100
100
  show_form
101
101
  end
@@ -1 +1 @@
1
- <%= label resource_name,(field.title || field.name), :id=>"field_#{field.__id__}_label" %>
1
+ <%= label resource_name, field.name, raw(field.title), :id=>"field_#{field.__id__}_label" %>
@@ -1,9 +1,3 @@
1
1
  <nav>
2
- <ul>
3
- <% Lolita.resources.each do |name,mapping| %>
4
- <li <%=mapping.to==resource_class ? "class='active'" : ""%> >
5
- <%= link_to mapping.name.to_s.humanize, lolita_resources_path(mapping) %>
6
- </li>
7
- <% end %>
8
- </ul>
2
+ <%= render_component :"lolita/navigation",:tree, :tree=>tree %>
9
3
  </nav>
@@ -0,0 +1,24 @@
1
+ <ul>
2
+ <%tree.branches.each do |branch|
3
+ if branch.object.is_a?(Lolita::Mapping)
4
+ branch.options[:url]||=lolita_resources_path(branch.object)
5
+ active=branch.object.to==resource_class
6
+ else
7
+
8
+ branch.options[:url]=if branch.options[:url].respond_to?(:call)
9
+ self.instance_eval(&(branch.options[:url]))
10
+ else
11
+ branch.options[:url]
12
+ end
13
+ active=branch.options[:url]==request.path
14
+ end
15
+
16
+ %>
17
+ <li <%=active ? "class='active'" : ""%>>
18
+ <%= link_to branch.title, branch.options[:url] %>
19
+ <% if branch.children.branches.any? %>
20
+ <%= render_component(:"lolita/navigation",:tree,:tree=>branch.children) %>
21
+ <% end %>
22
+ </li>
23
+ <% end %>
24
+ </ul>
@@ -29,7 +29,7 @@
29
29
  <div id="container">
30
30
  <%= render_component :"lolita/shared", :header %>
31
31
  <div id="main" role="main">
32
- <%= render_component :"lolita/navigation",:display %>
32
+ <%= render_component :"lolita/navigation",:display,:tree=>Lolita::Navigation::Tree[:"left_side_navigation"] %>
33
33
  <div id="content" class="<%= content_classes %>">
34
34
  <%= yield %>
35
35
  </div>
data/lib/lolita/hooks.rb CHANGED
@@ -126,6 +126,10 @@ module Lolita
126
126
  @callbacks={}
127
127
  end
128
128
 
129
+ def add_hooks *names
130
+ add_hook *names
131
+ end
132
+
129
133
  # This method is used to add hooks for class. It accept one or more hook names.
130
134
  # ====Example
131
135
  # add_hook :before_save
@@ -154,17 +158,17 @@ module Lolita
154
158
  # ====Example
155
159
  # MyClass.run(:before_save,:after_save,:scope=>MyClass.new)
156
160
  # # this will call callbacks in MyClass instance scope, that means that self will be MyClass instance.
157
- def run(*hook_names,&block)
161
+ def run(hook_name,*args,&block)
158
162
 
159
163
  result=nil
160
- options=hook_names.extract_options!
161
- (hook_names || []).each do |hook_name|
162
- raise Lolita::HookNotFound, "Hook #{hook_name} is not defined for #{self}." unless self.has_hook?(hook_name)
163
- in_hooks_scope(options[:scope],options[:run_scope]) do
164
- callback=get_callback(hook_name)
165
- result=run_callback(callback,&block)
166
- end
164
+ options=args ? args.extract_options! : {}
165
+
166
+ raise Lolita::HookNotFound, "Hook #{hook_name} is not defined for #{self}." unless self.has_hook?(hook_name)
167
+ in_hooks_scope(options[:scope],options[:run_scope]) do
168
+ callback=get_callback(hook_name)
169
+ result=run_callback(callback,&block)
167
170
  end
171
+
168
172
  result
169
173
  end
170
174
 
@@ -188,8 +192,10 @@ module Lolita
188
192
  # let_content # execute callback block(-s) in same scope as run is executed.
189
193
  # end
190
194
  def let_content
191
- if content=self.given_callback_content
195
+ if self.given_callback_content.respond_to?(:call)
192
196
  run_block(self.given_callback_content)
197
+ elsif self.given_callback_content
198
+ self.given_callback_content
193
199
  end
194
200
  end
195
201
 
@@ -239,19 +245,20 @@ module Lolita
239
245
 
240
246
  # Run blocks from <em>blocks</em> Array. Also it set #given_callback_content if block is given, this
241
247
  # will allow to call #let_content. Each block is runned with #run_block.
248
+ # After first run result of first block become #given_callback_content, and when next block
249
+ # call #let_content, this string will be returned for that block
242
250
  def run_blocks blocks,&given_block
243
251
  result=""
244
- (blocks||[]).each do |block|
245
- begin
246
- if block_given?
247
- self.given_callback_content=given_block
248
- end
252
+
253
+ self.given_callback_content=block_given? ? given_block : nil
254
+
255
+ if blocks && !blocks.empty?
256
+ blocks.each do |block|
249
257
  result << (run_block(block,&given_block)).to_s
250
- ensure
251
- self.given_callback_content=nil
258
+ self.given_callback_content=result
252
259
  end
253
- end
254
- if block_given? && (!blocks || (blocks && blocks.empty?))
260
+ elsif block_given?
261
+ self.given_callback_content=nil
255
262
  result << run_block(given_block).to_s
256
263
  end
257
264
  result
@@ -2,6 +2,117 @@ module Lolita
2
2
  module Navigation
3
3
  class Branch
4
4
 
5
+ attr_accessor :title,:name,:object
6
+ attr_reader :level,:options,:tree,:parent
7
+
8
+ def initialize(*args)
9
+
10
+ @options=args ? args.extract_options! : {}
11
+ set_object(args||[])
12
+ set_default_values
13
+ assign_attributes_from_options
14
+ end
15
+
16
+ def tree=(new_tree)
17
+ raise ArgumentError, "Tree already assigned" if self.tree || !new_tree.is_a?(Lolita::Navigation::Tree)
18
+ @tree=new_tree
19
+ end
20
+
21
+ def children
22
+ @children||=Lolita::Navigation::Tree.new("#{name}_children_tree")
23
+ @children
24
+ end
25
+
26
+ def index
27
+ self.tree.get_branch_index(self)
28
+ end
29
+
30
+ def siblings
31
+ index=self.index
32
+ {
33
+ :before=>self.tree.branches[index-1],
34
+ :after=>self.tree.branches[index+1]
35
+ }
36
+ end
37
+
38
+ def append(*args)
39
+ move_to(:append,*args)
40
+ end
41
+
42
+ def after(*args)
43
+ move_to(:after,*args)
44
+ end
45
+
46
+ def before(*args)
47
+ move_to(:before,*args)
48
+ end
49
+
50
+ def prepend(*args)
51
+ move_to(:prepend,*args)
52
+ end
53
+
54
+ def self.get_or_create(*args)
55
+ options=args ? args.extract_options! : {}
56
+ args||=[]
57
+ possible_object=args[0]
58
+
59
+ if possible_object.is_a?(String)
60
+ self.new(possible_object,options.merge(:title=>possible_object))
61
+ elsif possible_object.is_a?(self)
62
+ possible_object
63
+ else
64
+ self.new(possible_object,options)
65
+ end
66
+ end
67
+
68
+ def get_or_create(*args)
69
+ self.class.get_or_create(*args)
70
+ end
71
+
72
+ private
73
+
74
+ def move_to(position,*args)
75
+ branch=get_or_create(*args)
76
+ raise ArgumentError("Can't #{position} without branch.") unless branch
77
+ if [:before,:after].include?(position) && !branch.tree
78
+ raise ArgumentError, "Can't move in not-existing tree!"
79
+ end
80
+ new_branch=case position
81
+ when :append
82
+ self.children.append(branch)
83
+ when :prepend
84
+ self.children.prepend(branch)
85
+ when :after
86
+ branch.tree.before(branch,self)
87
+ when :before
88
+ branch.tree.after(branch,self)
89
+ end
90
+ new_branch.instance_variable_set(:"@level",new_branch.level+([:append,:prepend].include?(position) ? 1 : 0))
91
+ new_branch
92
+ end
93
+
94
+
95
+ def set_object(args)
96
+ @object=if args[0].is_a?(Lolita::Navigation::Tree)
97
+ @level=0
98
+ args[0]
99
+ else
100
+ args[0]
101
+ end
102
+ end
103
+
104
+ def set_default_values
105
+ @name||="branch_#{self.__id__}"
106
+ @level||=0
107
+ end
108
+
109
+ def assign_attributes_from_options
110
+ @options.each{|key,value|
111
+ if self.respond_to?(:"#{key}=")
112
+ self.send(:"#{key}=",@options.delete(key))
113
+ end
114
+ }
115
+ end
5
116
  end
6
117
  end
7
118
  end
@@ -1,6 +1,108 @@
1
1
  module Lolita
2
2
  module Navigation
3
3
  class Tree
4
+ include Enumerable
5
+ include Lolita::Hooks
6
+
7
+ class<<self
8
+ def remember(tree)
9
+ @@trees||={}
10
+ @@trees[tree.name.to_sym]=tree
11
+ end
12
+
13
+ def [](name)
14
+ @@trees||={}
15
+ @@trees[name]
16
+ end
17
+ end
18
+
19
+ add_hooks :before_branch_added, :after_branch_added
20
+
21
+ attr_reader :name,:root,:default_position,:branches
22
+
23
+ def initialize(name)
24
+ @name=name
25
+ @default_possition=:append
26
+ @branches=[]
27
+ end
28
+
29
+ def each
30
+ @branches.each do |branch|
31
+ yield branch
32
+ if branch.children.any?
33
+ branch.children.each do |child|
34
+ yield child
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ def method_missing method_name, *args
41
+ @branches.send(method_name.to_sym,*args)
42
+ end
43
+
44
+
45
+ def append(*branch)
46
+ adding_branch(*branch) do |fixed_branch|
47
+ @branches<<fixed_branch
48
+ end
49
+ end
50
+
51
+ def prepend(*branch)
52
+ adding_branch(*branch) do |fixed_branch|
53
+ @branches.unshift(fixed_branch)
54
+ end
55
+ end
56
+
57
+ def after(given_branch,*other_branch)
58
+ index=get_branch_index(given_branch)
59
+
60
+ adding_branch(*other_branch) do |fixed_branch|
61
+ put_in_branches(fixed_branch,index)
62
+ end
63
+ end
64
+
65
+ def before(given_branch,*other_branch)
66
+ index=get_branch_index(given_branch)
67
+
68
+ adding_branch(*other_branch) do |fixed_branch|
69
+ put_in_branches(fixed_branch,index-1)
70
+ end
71
+ end
72
+
73
+ def get_branch_index(given_branch)
74
+ @branches.each_with_index{|branch,index|
75
+ return index if given_branch==branch
76
+ }
77
+ raise ArgumentError, "Branch #{given_branch.inspect} not exists in #{self.inspect}"
78
+ end
79
+
80
+ private
81
+
82
+ def adding_branch *branch
83
+ self.run(:before_branch_added,*branch)
84
+ fixed_branch=fix_branch(*branch)
85
+ yield fixed_branch
86
+ @last_branch=fixed_branch
87
+ self.run(:after_branch_added,fixed_branch)
88
+ fixed_branch
89
+ end
90
+
91
+ def fix_branch(*branch)
92
+ unless branch[0].is_a?(Lolita::Navigation::Branch)
93
+ options=branch.extract_options!
94
+ Lolita::Navigation::Branch.get_or_create(*branch,options.merge(:tree=>self))
95
+ else
96
+ branch[0].tree=self
97
+ branch[0]
98
+ end
99
+ end
100
+
101
+ def put_in_branches branch,index
102
+ before_part=@branches.slice(0,index+1) || []
103
+ after_part=@branches.slice(index+1,@branches.size-index) || []
104
+ @branches=before_part+[branch]+after_part
105
+ end
4
106
 
5
107
  end
6
108
  end
@@ -1,13 +1,18 @@
1
1
  module ActionDispatch::Routing
2
2
 
3
- class RouteSet
4
- # alias_method_chain :draw, :lolita
3
+ class RouteSet
5
4
 
6
- # def draw_with_lolita
7
- # Lolita::Hooks.run("")
8
- # draw_without_lolita
9
- # end
5
+ def draw_with_lolita *args,&block
6
+ unless Lolita::Navigation::Tree[:"left_side_navigation"]
7
+ tree=Lolita::Navigation::Tree.new(:"left_side_navigation")
8
+ Lolita::Navigation::Tree.remember(tree)
9
+ end
10
+ draw_without_lolita *args,&block
11
+ end
12
+
13
+ alias_method_chain :draw, :lolita
10
14
  end
15
+
11
16
  class Mapper
12
17
 
13
18
  # Every module, that is used with lolita and has routes, need to have
@@ -51,6 +56,7 @@ class RouteSet
51
56
  mapping=Lolita.add_mapping(resource,options)
52
57
  Lolita.resources[mapping.name]=mapping
53
58
  target_class=mapping.to
59
+ Lolita::Navigation::Tree[:"left_side_navigation"].append(mapping,:title=>mapping.name.to_s.humanize)
54
60
  all_resource_classes<<target_class
55
61
 
56
62
  lolita_scope mapping.name do
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.2"
8
+ s.version = "3.1.3"
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-14}
12
+ s.date = %q{2011-04-15}
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 = [
@@ -72,6 +72,7 @@ Gem::Specification.new do |s|
72
72
  "app/views/components/lolita/configuration/tab/default/_display.html.erb",
73
73
  "app/views/components/lolita/configuration/tabs/_display.html.erb",
74
74
  "app/views/components/lolita/navigation/_display.html.erb",
75
+ "app/views/components/lolita/navigation/_tree.html.erb",
75
76
  "app/views/components/lolita/shared/_flash.html.erb",
76
77
  "app/views/components/lolita/shared/_header.html.erb",
77
78
  "app/views/components/lolita/shared/_right_sidebar.html.erb",
@@ -15,8 +15,8 @@ describe Lolita::Navigation::Branch do
15
15
  branch.name.should match(/branch/)
16
16
  end
17
17
 
18
- it "should not have level when branch is not in tree" do
19
- branch.level.should be_nil
18
+ it "should have level 0 when branch is not in tree" do
19
+ branch.level.should == 0
20
20
  end
21
21
 
22
22
  it "should have options, that may containe any values needed" do
@@ -30,27 +30,6 @@ describe Lolita::Navigation::Branch do
30
30
  branch.tree=Object.new
31
31
  }.should raise_error(ArgumentError)
32
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
33
  end
55
34
 
56
35
  describe "adding" do
@@ -60,7 +39,7 @@ describe Lolita::Navigation::Branch do
60
39
  new_branch.append(Lolita::Navigation::Branch.new)
61
40
  new_branch.append(Object)
62
41
  new_branch.append("Pages")
63
- new_branch.children.should have(3).items
42
+ new_branch.children.branches.should have(3).items
64
43
  end
65
44
 
66
45
  it "should prepend to exsiting branch" do
@@ -70,11 +49,35 @@ describe Lolita::Navigation::Branch do
70
49
  end
71
50
 
72
51
  it "should add before and after branch" do
73
- branch=tree.add(Object)
74
- branch.before(Lolita::Navigation::Branch.new)
75
- branch.after(Object)
52
+ branch=tree.append(Object)
53
+ first_branch=Lolita::Navigation::Branch.new
54
+ debugger
55
+ first_branch.before(branch)
56
+ last_branch=Lolita::Navigation::Branch.new
57
+ last_branch.after(branch)
76
58
  branch.siblings.values.compact.should have(2).items
77
59
  end
78
60
  end
79
61
 
62
+ describe "relationships" do
63
+ let(:branch){Lolita::Navigation::Branch.new}
64
+
65
+ it "should have parent" do
66
+ branch=tree.append(Object)
67
+ branch.parent.should == tree.root
68
+ end
69
+
70
+ it "should have childern" do
71
+ branch.append(Lolita::Navigation::Branch.new)
72
+ branch.children.class.should == Lolita::Navigation::Tree
73
+ branch.children.branches.should have(1).item
74
+ end
75
+
76
+ it "should have siblings" do
77
+ tree.append(Object)
78
+ branch=tree.append(String)
79
+ branch.siblings[:before].should_not be_nil
80
+ branch.siblings[:after].should be_nil
81
+ end
82
+ end
80
83
  end
@@ -18,16 +18,13 @@ describe Lolita::Navigation::Tree do
18
18
 
19
19
  end
20
20
 
21
- describe "work with nodes" do
22
- it "should have root branch with level 0" do
23
- tree.root.should_not be_nil
24
- tree.root.level.should == 0
21
+ describe "adding" do
22
+ it "should append" do
23
+ tree.append(Lolita::Navigation::Branch.new)
25
24
  end
26
- end
27
25
 
28
- context "#add" do
29
26
  it "should accept object, position and options" do
30
- tree.add(Object.new,:append,:url=>"/mypath")
27
+ tree.append(Object.new,:url=>"/mypath")
31
28
  tree.should have(1).branch
32
29
  end
33
30
  end
@@ -38,7 +35,7 @@ describe Lolita::Navigation::Tree do
38
35
 
39
36
  it "should iterate through all branches" do
40
37
  0.upto(3){|i|
41
- tree.add(Object.new,:append,:url=>"/#{i}",:name=>"branch#{i}")
38
+ tree.append(Object.new,:url=>"/#{i}",:name=>"branch#{i}")
42
39
  }
43
40
  tree.each_with_index do |branch,index|
44
41
  branch.name.should == "branch#{index}"
@@ -46,37 +43,32 @@ describe Lolita::Navigation::Tree do
46
43
  end
47
44
 
48
45
  it "should itereate in each level" do
49
- parent_branch=tree.root
50
46
  branch=empty_branch
51
47
  0.upto(5){|i|
52
- parent_branch.append(branch)
48
+ tree.append(branch)
53
49
  parent_branch=branch
54
50
  branch=Lolita::Navigation::Branch.new(Object,:url=>"/")
55
51
  }
56
- level_counter=1
57
- tree.each_level do |branches,level|
58
- branches.should have(1).item
59
- level.should == level_counter
60
- level_counter+=1
52
+ level_counter=0
53
+ tree.each do |branch|
54
+ branch.level.should == level_counter
61
55
  end
62
56
  end
63
57
  end
64
58
 
65
59
  it "should have callbacks" do
66
60
  Lolita::Navigation::Tree.should respond_to(:hooks)
67
- hook_names=[:before_load,:after_load,:before_branch_added,:after_branch_added]
61
+ hook_names=[:before_branch_added,:after_branch_added]
68
62
  (Lolita::Navigation::Tree.hooks - hook_names).should be_empty
69
63
  end
70
64
 
71
65
  it "should have way to add branches based on earlier added branches" do
72
66
  tree.after_branch_added do
73
- self.each do |branch|
74
- if branch.resources.is_a?(Object)
75
- branch.append(Object,:url=>"/")
76
- end
67
+ unless self.detect{|branch| branch.options[:url]=="/"}
68
+ self.append(Object,:url=>"/")
77
69
  end
78
70
  end
79
- tree.add(Object,:append,:url=>"/mypath")
71
+ tree.append(Object,:url=>"/mypath")
80
72
  tree.should have(2).branches
81
73
  end
82
74
  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.2
5
+ version: 3.1.3
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-14 00:00:00 +03:00
13
+ date: 2011-04-15 00:00:00 +03:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -232,6 +232,7 @@ files:
232
232
  - app/views/components/lolita/configuration/tab/default/_display.html.erb
233
233
  - app/views/components/lolita/configuration/tabs/_display.html.erb
234
234
  - app/views/components/lolita/navigation/_display.html.erb
235
+ - app/views/components/lolita/navigation/_tree.html.erb
235
236
  - app/views/components/lolita/shared/_flash.html.erb
236
237
  - app/views/components/lolita/shared/_header.html.erb
237
238
  - app/views/components/lolita/shared/_right_sidebar.html.erb
@@ -482,7 +483,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
482
483
  requirements:
483
484
  - - ">="
484
485
  - !ruby/object:Gem::Version
485
- hash: 43629427
486
+ hash: 1019725473
486
487
  segments:
487
488
  - 0
488
489
  version: "0"