loaf 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ 0.3.0 (February 25, 2012)
2
+
3
+ * Add loaf gem errors
4
+ * Add custom options validator for filtering invalid breadcrumbs params
5
+ * Renamed main gem helpers for adding breadcrumbs from `add_breadcrumb` to
6
+ `breadcrumb`, both inside controllers and views.
7
+ * Add specs for isolated view testing.
8
+
1
9
  0.2.1 (February 22, 2012)
2
10
 
3
11
  * Add more integration tests and fixed bug with adding breadcrumbs inside view
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- loaf (0.2.0)
4
+ loaf (0.2.1)
5
5
  rails
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -59,10 +59,10 @@ In controller:
59
59
  ```ruby
60
60
  class Blog::CategoriesController < ApplicationController
61
61
 
62
- add_breadcrumb 'Article Categories', 'blog_categories_path', :only => [:show]
62
+ breadcrumb 'Article Categories', 'blog_categories_path', :only => [:show]
63
63
 
64
64
  def show
65
- add_breadcrumb "#{@category.title}", 'blog_category_path(@category)'
65
+ breadcrumb "#{@category.title}", 'blog_category_path(@category)'
66
66
  end
67
67
  end
68
68
  ```
@@ -100,7 +100,7 @@ Therefore in your controller/view one would have
100
100
  ```ruby
101
101
  class Blog::CategoriesController < ApplicationController
102
102
 
103
- add_breadcrumb 'blog.categories', 'blog_categories_path'
103
+ breadcrumb 'blog.categories', 'blog_categories_path'
104
104
 
105
105
  end
106
106
 
@@ -0,0 +1,6 @@
1
+ en:
2
+ loaf:
3
+ errors:
4
+ messages:
5
+ invalid_options: "Invalid option :%{invalid}. Valid options are: %{valid}, make sure these are the ones you are using."
6
+
data/lib/loaf.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'loaf/configuration'
2
+ require 'loaf/errors'
2
3
  require 'loaf/railtie'
3
4
  require 'loaf/crumb'
4
5
  require 'loaf/builder'
@@ -6,6 +7,10 @@ require 'loaf/translation'
6
7
  require 'loaf/controller_extensions'
7
8
  require 'loaf/view_extensions'
8
9
  require 'loaf/crumb_formatter'
10
+ require 'loaf/options_validator'
11
+
12
+ # Add English load path by default
13
+ I18n.load_path << File.join(File.dirname(__FILE__), 'config', 'locales', 'en.yml')
9
14
 
10
15
  module Loaf
11
16
  extend Configuration
@@ -26,6 +26,11 @@ module Loaf
26
26
 
27
27
  DEFAULT_ROOT = true
28
28
 
29
+ # Sets the Loaf configuration options. Best used by passing a block.
30
+ #
31
+ # Loaf.configure do |config|
32
+ # config.capitalize = true
33
+ # end
29
34
  def configure
30
35
  yield self
31
36
  end
@@ -11,12 +11,13 @@ module Loaf
11
11
 
12
12
  module ClassMethods
13
13
 
14
- def add_breadcrumb(name, url, options = {})
14
+ def breadcrumb(name, url, options = {})
15
15
  before_filter(options) do |instance|
16
16
  # instance.send(:add_breadcrumb, _normalize_name(name), url)
17
17
  instance.send(:add_breadcrumb, name, url, options)
18
18
  end
19
19
  end
20
+ alias :add_breadcrumb :breadcrumb
20
21
 
21
22
  private
22
23
 
@@ -54,9 +55,10 @@ module Loaf
54
55
  end
55
56
  end
56
57
 
57
- def add_breadcrumb(name, url, options={})
58
+ def breadcrumb(name, url, options={})
58
59
  _breadcrumbs << Loaf::Crumb.new(name, url)
59
60
  end
61
+ alias :add_breadcrumb :breadcrumb
60
62
 
61
63
  def _breadcrumbs
62
64
  @_breadcrumbs ||= []
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+
3
+ module Loaf #:nodoc:
4
+
5
+ # Default Loaf error for all custom errors.
6
+ #
7
+ class LoafError < StandardError
8
+ BASE_KEY = "loaf.errors.messages"
9
+
10
+ def error_message(key, attributes)
11
+ translate(key, attributes)
12
+ end
13
+
14
+ def translate(key, options)
15
+ ::I18n.translate("#{BASE_KEY}.#{key}", { :locale => :en }.merge(options))
16
+ end
17
+ end
18
+
19
+ # Raised when invalid options are passed to breadcrumbs view renderer.
20
+ # InvalidOptions.new :name, :crumber, [:crumb]
21
+ #
22
+ class InvalidOptions < LoafError
23
+ def initialize(invalid, valid)
24
+ super(
25
+ error_message("invalid_options",
26
+ { :invalid => invalid, :valid => valid.join(', ') }
27
+ )
28
+ )
29
+ end
30
+ end
31
+
32
+ end # Loaf
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ require 'loaf/errors'
4
+
5
+ module Loaf
6
+ module OptionsValidator
7
+ def valid?(options)
8
+ valid_options = Loaf::Configuration::VALID_ATTRIBUTES
9
+ options.each_key do |key|
10
+ if !valid_options.include?(key)
11
+ raise Loaf::InvalidOptions.new(key, valid_options)
12
+ end
13
+ end
14
+ true
15
+ end
16
+ end # OptionsValidator
17
+ end # Loaf
data/lib/loaf/version.rb CHANGED
@@ -3,8 +3,8 @@
3
3
  module Loaf
4
4
  module Version
5
5
  MAJOR = 0
6
- MINOR = 2
7
- PATCH = 1
6
+ MINOR = 3
7
+ PATCH = 0
8
8
  BUILD = nil
9
9
 
10
10
  STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.');
@@ -1,23 +1,26 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'loaf/crumb_formatter'
4
+ require 'loaf/options_validator'
4
5
 
5
6
  module Loaf
6
7
  module ViewExtensions
7
8
  include Loaf::CrumbFormatter
9
+ include Loaf::OptionsValidator
8
10
 
9
- # Adds breadcrumbs in a view.
11
+ # Adds breadcrumbs inside view.
10
12
  #
11
- def add_breadcrumb(name, url)
13
+ def breadcrumb(name, url)
12
14
  _breadcrumbs.push Loaf::Crumb.new(name, url)
13
15
  end
16
+ alias :add_breadcrumb :breadcrumb
14
17
 
15
18
  # Renders breadcrumbs inside view.
16
19
  #
17
20
  def breadcrumbs(options={}, &block)
18
21
  #builder = Loaf::Builder.new(options)
22
+ valid? options
19
23
  options = Loaf.config.merge(options)
20
-
21
24
  _breadcrumbs.each do |crumb|
22
25
  name = format_name crumb, options
23
26
 
@@ -14,7 +14,9 @@ describe Loaf::ControllerExtensions do
14
14
  context 'classes extending controller_extensions' do
15
15
  subject { Controller }
16
16
  specify { should respond_to(:add_breadcrumb) }
17
+ specify { should respond_to(:breadcrumb) }
17
18
  specify { subject.new.should respond_to(:add_breadcrumb) }
19
+ specify { subject.new.should respond_to(:breadcrumb) }
18
20
  specify { subject.new.should respond_to(:add_breadcrumbs) }
19
21
  specify { subject.new.should respond_to(:clear_breadcrumbs) }
20
22
  end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Loaf::OptionsValidator do
4
+
5
+ let(:klass) { Class.extend Loaf::OptionsValidator }
6
+
7
+ describe '.valid?' do
8
+ context 'when the passed options are valid' do
9
+ let(:options) do
10
+ { :crumb_length => 10 }
11
+ end
12
+
13
+ it 'returns true' do
14
+ klass.valid?(options).should be_true
15
+ end
16
+ end
17
+ context 'when the passed optiosn are invalid' do
18
+ let(:options) do
19
+ { :invalid_param => true }
20
+ end
21
+
22
+ it 'raises an error' do
23
+ expect do
24
+ klass.valid? options
25
+ end.to raise_error(Loaf::InvalidOptions)
26
+ end
27
+ end
28
+ end
29
+ end # Loaf::OptionsValidator
@@ -0,0 +1,114 @@
1
+ require 'spec_helper'
2
+
3
+ describe Loaf::ViewExtensions do
4
+
5
+ class View < ActionView::Base
6
+ include Loaf::ViewExtensions
7
+ attr_accessor_with_default :_breadcrumbs, []
8
+ end
9
+
10
+ let(:name) { 'name' }
11
+ let(:url) { 'url' }
12
+ let(:view) { View }
13
+ let(:instance) { view.new }
14
+
15
+ context 'classes extending view_extensions' do
16
+ subject { View.new }
17
+ specify { should respond_to(:breadcrumb) }
18
+ specify { should respond_to(:add_breadcrumb) }
19
+ specify { should respond_to(:breadcrumbs) }
20
+ end
21
+
22
+ context 'adding view breadcrumb' do
23
+ it 'calls breadcrumbs helper' do
24
+ instance.should_receive(:_breadcrumbs).and_return []
25
+ instance.breadcrumb name, url
26
+ end
27
+
28
+ it 'creates crumb instance' do
29
+ Loaf::Crumb.should_receive(:new).with(name, url)
30
+ instance.breadcrumb name, url
31
+ end
32
+
33
+ it 'should add crumb to breadcrumbs storage' do
34
+ expect do
35
+ instance.breadcrumb name, url
36
+ end.to change { instance._breadcrumbs.size }.by(1)
37
+ end
38
+ end
39
+
40
+ context 'breadcrumbs rendering' do
41
+
42
+ let(:block) { lambda { |name, url, styles| } }
43
+
44
+ before do
45
+ @crumbs = []
46
+ @crumbs << Loaf::Crumb.new('home', :home_path)
47
+ @crumbs << Loaf::Crumb.new('posts', :posts_path)
48
+ instance.stub(:_breadcrumbs).and_return @crumbs
49
+
50
+ @options = {}
51
+ Loaf.stub_chain(:config, :merge).and_return @options
52
+ @crumbs.each do |crumb|
53
+ instance.stub(:format_name).with(crumb, @options).and_return crumb.name
54
+ instance.stub(:_process_url_for).with(crumb.url).and_return crumb.name
55
+ end
56
+ instance.stub(:current_page?).and_return true
57
+ end
58
+
59
+ it 'validates passed options' do
60
+ instance.should_receive(:valid?)
61
+ instance.breadcrumbs &block
62
+ end
63
+
64
+ it 'raises error for unkown option' do
65
+ expect do
66
+ instance.breadcrumbs :unsupported => true, &block
67
+ end.to raise_error(Loaf::InvalidOptions)
68
+ end
69
+
70
+ it 'calls loaf configuration' do
71
+ Loaf.should_receive(:config).and_return @options
72
+ @options.should_receive(:merge).with({:crumb_length => 10}).
73
+ and_return @options
74
+ instance.breadcrumbs :crumb_length => 10, &block
75
+ end
76
+
77
+ it 'uses breadcrumbs collection helper' do
78
+ instance.should_receive(:_breadcrumbs)
79
+ instance.breadcrumbs &block
80
+ end
81
+
82
+ it 'retrieves crumbs collection' do
83
+ instance._breadcrumbs.should eql @crumbs
84
+ instance.breadcrumbs &block
85
+ end
86
+
87
+ it 'formats crumb name' do
88
+ instance.should_receive(:format_name)
89
+ instance.breadcrumbs &block
90
+ end
91
+
92
+ it 'checks if crumb url is current' do
93
+ instance.should_receive(:current_page?)
94
+ instance.breadcrumbs &block
95
+ end
96
+
97
+ context 'rendering inside block' do
98
+ it 'returns crumb attributes' do
99
+ crumb = Loaf::Crumb.new name, url
100
+ instance.stub(:_breadcrumbs).and_return [crumb]
101
+ instance.should_receive(:format_name).with(crumb, @options).
102
+ and_return crumb.name
103
+ instance.should_receive(:_process_url_for).with(crumb.url).
104
+ and_return crumb.name
105
+
106
+ instance.breadcrumbs do |name, url, styles|
107
+ name.should eql crumb.name
108
+ url.should eql crumb.name
109
+ styles.should be_blank
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end # Loaf::ViewExtensions
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loaf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-22 00:00:00.000000000 Z
12
+ date: 2012-02-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &2152603000 !ruby/object:Gem::Requirement
16
+ requirement: &2160425740 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2152603000
24
+ version_requirements: *2160425740
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rails
27
- requirement: &2152602060 !ruby/object:Gem::Requirement
27
+ requirement: &2160424420 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '3.1'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2152602060
35
+ version_requirements: *2160424420
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
- requirement: &2152600360 !ruby/object:Gem::Requirement
38
+ requirement: &2160422760 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2152600360
46
+ version_requirements: *2160422760
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec-rails
49
- requirement: &2152598720 !ruby/object:Gem::Requirement
49
+ requirement: &2160420740 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2152598720
57
+ version_requirements: *2160420740
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: capybara
60
- requirement: &2152595720 !ruby/object:Gem::Requirement
60
+ requirement: &2160419820 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2152595720
68
+ version_requirements: *2160419820
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: bundler
71
- requirement: &2152594960 !ruby/object:Gem::Requirement
71
+ requirement: &2160418980 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2152594960
79
+ version_requirements: *2160418980
80
80
  description: Loaf helps you manage breadcrumbs in your rails app. It aims to handle
81
81
  crumb data through easy dsl and expose it through view helpers without any assumptions
82
82
  about markup.
@@ -98,12 +98,15 @@ files:
98
98
  - README.md
99
99
  - Rakefile
100
100
  - init.rb
101
+ - lib/config/locales/en.yml
101
102
  - lib/loaf.rb
102
103
  - lib/loaf/builder.rb
103
104
  - lib/loaf/configuration.rb
104
105
  - lib/loaf/controller_extensions.rb
105
106
  - lib/loaf/crumb.rb
106
107
  - lib/loaf/crumb_formatter.rb
108
+ - lib/loaf/errors.rb
109
+ - lib/loaf/options_validator.rb
107
110
  - lib/loaf/railtie.rb
108
111
  - lib/loaf/translation.rb
109
112
  - lib/loaf/version.rb
@@ -114,7 +117,9 @@ files:
114
117
  - spec/integration/nested_crumbs_spec.rb
115
118
  - spec/loaf/controller_extensions_spec.rb
116
119
  - spec/loaf/crumb_formatter_spec.rb
120
+ - spec/loaf/options_validator_spec.rb
117
121
  - spec/loaf/translation_spec.rb
122
+ - spec/loaf/view_extensions_spec.rb
118
123
  - spec/loaf_spec.rb
119
124
  - spec/rails_app/.gitignore
120
125
  - spec/rails_app/Rakefile
@@ -191,7 +196,9 @@ test_files:
191
196
  - spec/integration/nested_crumbs_spec.rb
192
197
  - spec/loaf/controller_extensions_spec.rb
193
198
  - spec/loaf/crumb_formatter_spec.rb
199
+ - spec/loaf/options_validator_spec.rb
194
200
  - spec/loaf/translation_spec.rb
201
+ - spec/loaf/view_extensions_spec.rb
195
202
  - spec/loaf_spec.rb
196
203
  - spec/rails_app/.gitignore
197
204
  - spec/rails_app/Rakefile