easycrumbs 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Easy breadcrumbs for your site
4
4
 
5
- * gemcutter[not yet]
5
+ * gemcutter[http://rubygems.org/gems/easycrumbs]
6
6
  * repository[http://github.com/staszek/easycrumbs]
7
7
 
8
8
  == Installation
@@ -12,11 +12,180 @@ If you don't have the {Gemcutter sources}[http://gemcutter.org/pages/gem_docs] y
12
12
 
13
13
  To install the gem type:
14
14
  gem install easycrumbs
15
+
16
+ Add it to your Gemfile
17
+ gem "easycrumbs"
15
18
 
16
- == Usage
19
+ == Quick start
17
20
 
18
- It does not work yet.
21
+ It is really easy to have breadcrumbs in your project. Just type this into any view file and breadcrumbs will work for you.
22
+ breadcrumbs
19
23
 
20
- == Copyright
24
+ ERB example:
25
+ <div id=breadcrumbs>
26
+ <%= breadcrumbs %>
27
+ </div>
28
+ HAML example:
29
+ #breadcrumbs
30
+ = breadcrumbs
31
+
32
+ You do not need to specify any more options in models or controllers. Easycrumbs will take everything it needs from your routes.rb file.
33
+
34
+ The routes.rb file with
35
+ map.resources :countries do |country|
36
+ country.resources :movies do |movie|
37
+ movie.resources :actors
38
+ end
39
+ end
40
+ could generate this breadcrumb
41
+ Home > Countries > Country > Movies > Movie > Actors > Edit Actor
42
+
43
+ == Model names
44
+ === From database
45
+ If your model has breadcrumb column in database then it will be used to generate breadcrumbs
46
+ @usa = Country.new(:breadcrumb => "Usa")
47
+ @titanic = Movie.new(:breadcrumb => "Titanic")
48
+ @leo = Actor.new(:breadcumb => "Leonardo Di Caprio")
49
+
50
+ Home > Countries > Usa > Movies > Titanic > Actors > Edit Leonardo Di Caprio
51
+
52
+ You can change column name using :name_column option
53
+ breadcrumbs :name_column => :seo
54
+
55
+ @usa = Country.new(:seo => "Usa")
56
+ @titanic = Movie.new(:seo => "Titanic")
57
+ @leo = Actor.new(:seo => "Leonardo Di Caprio")
58
+
59
+ Home > Countries > Usa > Movies > Titanic > Actors > Edit Leonardo Di Caprio
60
+
61
+ === From custom method
62
+ If you need custom name then just create method named breadcrumb in your model
63
+ class Actor < ActiveRecord::Base
64
+ def breadcrumb
65
+ "the best male actor: " + first_name
66
+ end
67
+ end
68
+
69
+ Home > Countries > Usa > Movies > Titanic > Actors > Edit the best male actor: Leonardo
70
+
71
+ === From i18n
72
+ Easycrumbs fully support i18n
73
+ breadcrumbs :i18n => true
74
+
75
+ Name will be taken from this key if your model does not have breadcrumb column or method:
76
+ breadcrumbs.models.model_name
77
+ Example en.yml
78
+ en:
79
+ breadcrumbs:
80
+ models:
81
+ country: C0untry
82
+ movie: M0v13
83
+ actor: 4ct0r
84
+
85
+ Home > Countries > C0untry > Movies > M0v13 > Actors > Edit 4act0r
21
86
 
87
+ == Controller names
88
+ ===From controller name
89
+ Default behavior is to use controller name
90
+ class CountriesController < ApplicationController
91
+ class MoviesController < ApplicationController
92
+ class ActorsController < ApplicationController
93
+
94
+ Home > Countries > Usa > Movies > Titanic > Actors > Edit Leonardo Di Caprio
95
+
96
+ === From custom method
97
+ Similar to models you can specify custom breadcrumb method
98
+ class MoviesController < ApplicationController
99
+ def breadcrumb
100
+ "Movies (#{collection.size})"
101
+ end
102
+ end
103
+
104
+ Home > Countries > Usa > Movies (234) > Titanic > Actors > Edit Leonardo Di Caprio
105
+
106
+ === From i18n
107
+ Controller can also use i18n names
108
+ breadcrumbs :i18n => true
109
+
110
+ Name will be taken from this key if your controller does not have breadcrumb method:
111
+ breadcrumbs.controllers.controller_name
112
+ Example en.yml
113
+ en:
114
+ breadcrumbs:
115
+ controllers:
116
+ application: H0m3
117
+ countries: C0untr135
118
+ movies: M0v135
119
+ actors: 4ct0r5
120
+
121
+ H0m3 > C0untr135 > Usa > M0v135 > Titanic > 4ct0r5 > Edit Leonardo Di Caprio
122
+
123
+ == Action prefixes
124
+ For last element you can add current action name as prefix.
125
+ Example with Editing actor object:
126
+ Home > Countries > Usa > Movies > Titanic > Actors > Edit Leonardo Di Caprio
127
+
128
+ === Default
129
+ For default prefix is added only for new and edit actions.
130
+ Home > Countries > Usa > Movies > Titanic > Actors > Edit Leonardo Di Caprio
131
+ Home > Countries > Usa > Movies > Titanic > Actors > New Leonardo Di Caprio
132
+
133
+ === Custom
134
+ You can change this behavior. Just set :prefix option as you want:
135
+ * :every - prefix will be added for every action
136
+ breadcrumbs :prefix => :every
137
+ action show => Actors > Show Leonardo Di Caprio
138
+ action new => Actors > New Leonardo Di Caprio
139
+ action edit => Actors > Edit Leonardo Di Caprio
140
+ * :none - prefix will not be added to any action
141
+ breadcrumbs :prefix => :none
142
+ action show => Actors > Leonardo Di Caprio
143
+ action new => Actors > Leonardo Di Caprio
144
+ action edit => Actors > Leonardo Di Caprio
145
+ * array of action names - prefix will be added only for specified actions
146
+ breadcrumbs :prefix => [:show, :new]
147
+ action show => Actors > Show Leonardo Di Caprio
148
+ action new => Actors > New Leonardo Di Caprio
149
+ action edit => Actors > Leonardo Di Caprio
150
+
151
+ === From i18n
152
+ Action names can also be taken from i18n
153
+ breadcrumbs :i18n => true
154
+
155
+ Name will be taken from this key:
156
+ breadcrumbs.actions.action_name
157
+ Example en.yml
158
+ en:
159
+ breadcrumbs:
160
+ actions:
161
+ new: N3w {{name}}
162
+ edit: 3d1t {{name}}
163
+ show: {{name}} 5h0w
164
+
165
+ == Blank links option
166
+ When path will not be recognized then exception will be raised
167
+ EasyCrumbs::NoPath
168
+ If you would like to have blank link(just text, without hyperlink) instead then just set :blank_links option
169
+ breadcrumbs :blank_links => true
170
+
171
+ == Rendering options
172
+ You can set some rendering options also:
173
+
174
+ === Separator
175
+ Default breadcrumbs separator is set to " > ", but you can use something else if you want
176
+ breadcrumbs :separator => ' ==> '
177
+ Home ==> Countries ==> Usa ==> Movies ==> Titanic ==> Actors ==> Edit Leonardo Di Caprio
178
+
179
+ breadcrumbs :separator => '/'
180
+ Home/Countries/Usa/Movies/Titanic/Actors/Edit Leonardo Di Caprio
181
+
182
+ === Last element as hyperlink
183
+ By default last element will always be render as hyperlink. If you would like to render it in plain text then use :last_link option
184
+ breadcrumbs :last_link => false
185
+ <a href="/actors"/>Actors</a> > Edit Leonardo Di Caprio
186
+
187
+ breadcrumbs :last_link => true
188
+ <a href="/actors"/>Actors</a> > <a href="/actors/1/edit">Edit Leonardo Di Caprio</a>
189
+
190
+ == Copyright
22
191
  Copyright (c) 2010 Stanisław Kolarzowski. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 1.0.0
data/easycrumbs.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{easycrumbs}
8
- s.version = "0.1.0"
8
+ s.version = "1.0.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Stanis\305\202aw Kolarzowski"]
12
- s.date = %q{2010-08-08}
12
+ s.date = %q{2010-10-30}
13
13
  s.description = %q{Easy breadcrumbs for your website}
14
14
  s.email = %q{stanislaw.kolarzowski@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -1,24 +1,24 @@
1
1
  module EasyCrumbs
2
2
  class Breadcrumb
3
3
  attr_reader :object, :name, :path
4
-
4
+
5
5
  # Breadcrumb object:
6
6
  # object - just object from application. Could be a model or controller
7
7
  # name - printed name
8
8
  # path - path to this object
9
- def initialize(object, options = {})
9
+ def initialize(object, options = {})
10
10
  @object = set_object(object)
11
11
  @name = set_name(options)
12
12
  @path = set_path(options[:path], options[:blank_links])
13
13
  end
14
-
15
-
14
+
15
+
16
16
  # Object from application must be a model or controller
17
17
  def set_object(object)
18
18
  raise EasyCrumbs::InvalidObject unless object.is_a?(ActionController::Base) || object.is_a?(ActiveRecord::Base)
19
19
  object
20
20
  end
21
-
21
+
22
22
  # Set name for model or controller
23
23
  def set_name(options = {})
24
24
  if object.is_a?(ActiveRecord::Base)
@@ -29,29 +29,36 @@ module EasyCrumbs
29
29
  end
30
30
  add_prefix(name, options[:action], options[:prefix], options[:i18n])
31
31
  end
32
-
32
+
33
33
  # Set name for model
34
34
  # Model has to have column equal to name_column
35
35
  def name_for_model(name_column, i18n)
36
- raise EasyCrumbs::NoName.new(@object.class, name_column) unless @object.respond_to? name_column
37
- name = @object.send name_column
38
- name.nil? ? name_for_nil(@object, i18n) : name
39
- end
40
-
41
- # Set name for object if it is nil
42
- def name_for_nil(object, i18n)
43
- i18n == true ? I18n.t("breadcrumbs.models.#{object.class.to_s.downcase}") : @object.class.to_s
36
+ if @object.respond_to? name_column
37
+ @object.send name_column
38
+ else
39
+ i18n == true ? I18n.t("breadcrumbs.models.#{object.class.to_s.downcase}") : default_model_name
40
+ end
44
41
  end
45
-
42
+
46
43
  # Set name for controller
47
44
  def name_for_controller(i18n)
48
45
  if @object.respond_to? :breadcrumb
49
46
  @object.breadcrumb
50
47
  else
51
- i18n == true ? I18n.t("breadcrumbs.controllers.#{@object.controller_name}") : @object.controller_name.titlecase
48
+ i18n == true ? I18n.t("breadcrumbs.controllers.#{@object.controller_name}") : default_controller_name
52
49
  end
53
50
  end
54
-
51
+
52
+ #Return default name for model object
53
+ def default_model_name
54
+ @object.class.to_s
55
+ end
56
+
57
+ # Return default name for controller object
58
+ def default_controller_name
59
+ @object.class == ApplicationController ? "Home" : @object.controller_name.titlecase
60
+ end
61
+
55
62
  # Add specyfic prefix if action is passed
56
63
  # prefix =
57
64
  # :every - add prefix for every action
@@ -75,12 +82,12 @@ module EasyCrumbs
75
82
  end
76
83
  name
77
84
  end
78
-
79
- # Return name of action.
85
+
86
+ # Return name of action.
80
87
  def action_name(action, i18n, name)
81
88
  i18n == true ? I18n.t("breadcrumbs.actions.#{action}", :name => name) : "#{action.titlecase} #{name}"
82
89
  end
83
-
90
+
84
91
  # Set path using hash from ActionController::Routing::Routes.recognize_path
85
92
  # Example looks like:
86
93
  # {:country_id => "1", :movie_id => "1", :id => "1", :action => "show", :controller => "movies"}
@@ -90,6 +97,6 @@ module EasyCrumbs
90
97
  raise EasyCrumbs::NoPath.new(e.message) unless blank_links == true
91
98
  nil
92
99
  end
93
-
100
+
94
101
  end
95
102
  end
@@ -4,29 +4,18 @@ module EasyCrumbs
4
4
  "object should be Controller(ActionController::Base) or Model(ActiveRecord::Base)"
5
5
  end
6
6
  end
7
-
8
- class NoName < StandardError
9
- def initialize(object_class, column)
10
- @object_class = object_class
11
- @column = column
12
- end
13
-
14
- def message
15
- "Can not set name. Model #{@object_class} does not have column \"#{@column}\". Try change column name or create \"#{@column}\" method"
16
- end
17
- end
18
-
7
+
19
8
  class NoPath < StandardError
20
9
  def initialize(routing_error)
21
10
  @routing_error = routing_error
22
11
  end
23
-
12
+
24
13
  def message
25
14
  "Can not set path. You can use :blank_links to return nil for no-recognized pathes. RoutingError: #{@routing_error}"
26
15
  end
27
16
  end
28
-
29
- class NotRecognized < StandardError
17
+
18
+ class NotRecognized < StandardError
30
19
  def message
31
20
  "Can not recognize main path."
32
21
  end
data/test/helper.rb CHANGED
@@ -13,20 +13,19 @@ require 'easycrumbs'
13
13
 
14
14
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
15
15
  ActiveRecord::Base.configurations = true
16
-
16
+
17
17
  ActiveRecord::Schema.verbose = false
18
18
  ActiveRecord::Schema.define(:version => 1) do
19
19
  create_table :countries do |t|
20
20
  t.string :name
21
21
  t.string :breadcrumb
22
22
  end
23
-
23
+
24
24
  create_table :movies do |t|
25
25
  t.string :name
26
26
  t.integer :country_id
27
- t.string :breadcrumb
28
27
  end
29
-
28
+
30
29
  create_table :actors do |t|
31
30
  t.string :first_name
32
31
  t.string :last_name
@@ -50,7 +49,7 @@ end
50
49
 
51
50
  class Actor < ActiveRecord::Base
52
51
  belongs_to :movie
53
-
52
+
54
53
  def breadcrumb
55
54
  "#{first_name} #{last_name}"
56
55
  end
@@ -4,7 +4,7 @@ class TestEasycrumbs < Test::Unit::TestCase
4
4
  context "EasyCrumbs tests" do
5
5
  setup do
6
6
  @usa = Country.create(:name => "USA", :breadcrumb => "United States of America")
7
- @titanic = @usa.movies.create(:name => "Titanic", :breadcrumb => "Titanic")
7
+ @titanic = @usa.movies.create(:name => "Titanic")
8
8
  @leo = @titanic.actors.create(:first_name => "Leonardo", :last_name => "Di Caprio")
9
9
  end
10
10
 
@@ -36,7 +36,7 @@ class TestEasycrumbs < Test::Unit::TestCase
36
36
 
37
37
  context "set name" do
38
38
  context "for model" do
39
- should "return breadcrumb column by default" do
39
+ should "return breadcrumb column by default if it has breadcrumb column" do
40
40
  assert_equal("United States of America", Breadcrumb.new(@usa).name)
41
41
  end
42
42
 
@@ -48,15 +48,11 @@ class TestEasycrumbs < Test::Unit::TestCase
48
48
  assert_equal("Leonardo Di Caprio", Breadcrumb.new(@leo).name)
49
49
  end
50
50
 
51
- should "raise exception if can not find name" do
52
- assert_raise(NoName) { Breadcrumb.new(@leo, :name_column => "wrong_column")}
53
- end
54
-
55
- should "return model name if column return nil" do
51
+ should "return model name if it not respond to name_column method and i18n is turned off" do
56
52
  assert_equal("Movie", Breadcrumb.new(Movie.new).name)
57
53
  end
58
54
 
59
- should "return model name from i18n if column return nil" do
55
+ should "return model name from i18n if it not respond to name_column method and i18n is turned on" do
60
56
  I18n.expects(:t).with("breadcrumbs.models.movie").returns("Das film")
61
57
  assert_equal("Das film", Breadcrumb.new(Movie.new, :i18n => true).name)
62
58
  end
@@ -67,9 +63,18 @@ class TestEasycrumbs < Test::Unit::TestCase
67
63
  assert_equal("Movies", Breadcrumb.new(MoviesController.new).name)
68
64
  end
69
65
 
66
+ should "return Home for ApplicationController" do
67
+ assert_equal("Home", Breadcrumb.new(ApplicationController.new).name)
68
+ end
69
+
70
70
  should "return breadcrumb method from controller" do
71
71
  assert_equal("Countries list", Breadcrumb.new(CountriesController.new).name)
72
72
  end
73
+
74
+ should "return transalted name for controller when i18n is turned on" do
75
+ I18n.expects(:t).with("breadcrumbs.controllers.movies").returns("la movies")
76
+ assert_equal("la movies", Breadcrumb.new(MoviesController.new, :i18n => true).name)
77
+ end
73
78
  end
74
79
 
75
80
  context "with prefix option" do
@@ -92,15 +97,8 @@ class TestEasycrumbs < Test::Unit::TestCase
92
97
  should "return only name if action is not in prefix array" do
93
98
  assert_equal("Leonardo Di Caprio", Breadcrumb.new(@leo, :action => "show", :prefix => [:destroy, :edit]).name)
94
99
  end
95
- end
96
-
97
- context "with i18n enable" do
98
- should "return transalted name for controller" do
99
- I18n.expects(:t).with("breadcrumbs.controllers.movies").returns("la movies")
100
- assert_equal("la movies", Breadcrumb.new(MoviesController.new, :i18n => true).name)
101
- end
102
100
 
103
- should "return transalted action as a prefix" do
101
+ should "return transalted action as a prefix when i18n is turned on" do
104
102
  name = "Leonardo Di Caprio"
105
103
  I18n.expects(:t).with("breadcrumbs.actions.edit", {:name => name}).returns("Editzione #{name}")
106
104
  assert_equal("Editzione Leonardo Di Caprio", Breadcrumb.new(@leo, :i18n => true, :action => "edit").name)
@@ -293,9 +291,7 @@ class TestEasycrumbs < Test::Unit::TestCase
293
291
 
294
292
  should "return array of breadcrumbs objects" do
295
293
  assert_equal(@collection.objects.size + 1, @results.size)
296
- results = @results.map(&:class).uniq
297
- assert_equal(1, results.size)
298
- assert_equal(EasyCrumbs::Breadcrumb, results.first)
294
+ @results.map.uniq == [EasyCrumbs::Breadcrumb]
299
295
  end
300
296
 
301
297
  should "last breadcrumb have name with action prefix" do
@@ -305,11 +301,11 @@ class TestEasycrumbs < Test::Unit::TestCase
305
301
 
306
302
  context "render" do
307
303
  setup do
308
- @results = [ "<a href=\"/\">Application</a>",
304
+ @results = [ "<a href=\"/\">Home</a>",
309
305
  "<a href=\"/countries\">Countries list</a>",
310
306
  "<a href=\"/countries/#{@usa.id}\">United States of America</a>",
311
307
  "<a href=\"/countries/#{@usa.id}/movies\">Movies</a>",
312
- "<a href=\"/countries/#{@usa.id}/movies/#{@titanic.id}\">Titanic</a>",
308
+ "<a href=\"/countries/#{@usa.id}/movies/#{@titanic.id}\">Movie</a>",
313
309
  "<a href=\"/countries/#{@usa.id}/movies/#{@titanic.id}/actors\">Actors</a>",
314
310
  "<a href=\"/countries/#{@usa.id}/movies/#{@titanic.id}/actors/#{@leo.id}\">Leonardo Di Caprio</a>"]
315
311
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easycrumbs
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
- - 0
8
7
  - 1
9
8
  - 0
10
- version: 0.1.0
9
+ - 0
10
+ version: 1.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Stanis\xC5\x82aw Kolarzowski"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-08 00:00:00 +02:00
18
+ date: 2010-10-30 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency