simple_breadcrumbs 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c657f2df38cd282d1f14ea5af190e4d63819189b
4
+ data.tar.gz: 7a0892b36489537f399471243e159f72fdead9b1
5
+ SHA512:
6
+ metadata.gz: ab9a308d04b6dbcf292aa4f3b84ab804330ee39d8700606098ba7730a4504767828d88982ef0cc1d7c52b621a08e27f3cf23e4a0f785efd0971151da8b790c29
7
+ data.tar.gz: 49a333a5f08a37eefffc5f42f832697512afd2e7c7a31407c578cf3e26a9dd088bc4d04dc3e504ea7854f3dfbb58fb73b378d4b44da34d09996516905db3f5b8
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in has_breadcrumb.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Squaremouth Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # SimpleBreadcrumbs
2
+
3
+ This gem provides a simple and flexible way to create breadcrumbs with Rails active record
4
+ models. Setting `has_breadcrumb` on a model will enable a view method,
5
+ `breadcrumb()`, which will show the links to the page and its parents in
6
+ a breadcrumb format.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'simple_breadcrumbs'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ ## Usage
19
+
20
+ In your model:
21
+
22
+ class User < ActiveRecord::Base
23
+ has_breadcrumb :title => :username
24
+ end
25
+
26
+ To display a model as a child of another model, you state the parent
27
+ by using the `:parent` parameter:
28
+
29
+ class UserProfile < ActiveRecord::Base
30
+ has_breadcrumb :parent => :user, :title => "Profile"
31
+ end
32
+
33
+ As can be seen here, `has_breadcrumb` can accept a `String` on the
34
+ `:title` option, which is useful in the case where there is no field in
35
+ the model which would be suitable. The `:parent` option however, must
36
+ take a `Symbol`.
37
+
38
+ Breadcrumb links are displayed in a view by including the `breadcrumb` erb tag.
39
+ For example in an erb view for creating a new user,
40
+
41
+ <h2 class="subtitle"><%= breadcrumb @user, 'New' %></h2>
42
+
43
+ will provide a link which includes the Index page and the controller
44
+ action to which this page is related.
45
+
46
+ Other options allowed by the `breadcrumb` method include
47
+ `:forced_parent`, which links to a non-natural parent of the current
48
+ view page.
49
+
50
+ By default, `breadcrumb` includes a link to the index of an object when only
51
+ one level exists.
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,24 @@
1
+ module HasBreadcrumb
2
+ module ClassMethods
3
+ def has_breadcrumb options = {}
4
+ define_method :breadcrumb_parent do
5
+ return nil if options[:parent].nil?
6
+ self.send(options[:parent])
7
+ end
8
+
9
+ define_method :breadcrumb_name do
10
+ return "" if options[:title].nil?
11
+ case options[:title].class.name
12
+ when "Symbol"
13
+ self.send(options[:title])
14
+ else
15
+ options[:title]
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ def self.included(receiver)
22
+ receiver.extend ClassMethods
23
+ end
24
+ end
@@ -0,0 +1,8 @@
1
+ module HasBreadcrumb
2
+ class Railtie < Rails::Railtie
3
+ config.before_initialize do
4
+ ActiveRecord::Base.class_eval { include HasBreadcrumb }
5
+ ActionView::Base.class_eval { include ShowBreadcrumb }
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,33 @@
1
+ module ShowBreadcrumb
2
+ # Recursive function to lookup through parent breadcrumbs
3
+ def collect_crumbs(obj, crumbs=[])
4
+ crumbs << obj
5
+ crumbs = collect_crumbs(obj.breadcrumb_parent, crumbs) unless obj.breadcrumb_parent.nil?
6
+ crumbs
7
+ end
8
+
9
+ # View helper to generate breadcrumbs
10
+ def breadcrumb(obj, action = nil, options = {})
11
+ crumb_html = ""
12
+ crumbs = collect_crumbs(obj)
13
+
14
+ if options.has_key?(:forced_parent)
15
+ crumb_link = url_for :controller => options[:forced_parent].class.to_s.underscore.pluralize, :action => "show", :id => options[:forced_parent].id
16
+ crumb_html = link_to options[:forced_parent].breadcrumb_name, crumb_link
17
+ elsif crumbs.length == 1
18
+ crumb_link = url_for :controller => obj.class.to_s.underscore.pluralize
19
+ crumb_html = link_to obj.class.model_name.human.pluralize, crumb_link
20
+ end
21
+
22
+ crumbs.reverse.each do |crumb_obj|
23
+ unless crumb_obj.id.nil?
24
+ crumb_html += ' > ' if crumb_html.length > 0
25
+ crumb_link = url_for :controller => crumb_obj.class.to_s.underscore.pluralize, :action => "show", :id => crumb_obj.id
26
+ crumb_html += link_to crumb_obj.breadcrumb_name, crumb_link
27
+ end
28
+ end
29
+
30
+ crumb_html += ' > ' + action + ' ' + obj.class.model_name.human.titleize if !action.nil?
31
+ crumb_html.html_safe
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module HasBreadcrumb
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ require "has_breadcrumb/version"
2
+ require "has_breadcrumb/has_breadcrumb.rb"
3
+ require "has_breadcrumb/show_breadcrumb.rb"
4
+ require "has_breadcrumb/railtie" if defined?(Rails)
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'has_breadcrumb/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "simple_breadcrumbs"
8
+ gem.version = HasBreadcrumb::VERSION
9
+ gem.platform = Gem::Platform::RUBY
10
+ gem.authors = ["Tim Harvey", "Matt Outten"]
11
+ gem.email = ["developers@squaremouth.com"]
12
+ gem.description = %q{Provides a simple and flexible way to create breadcrumbs with Rails active record models.}
13
+ gem.summary = %q{Setting has_breadcrumb on a model will enable a view method, breadcrumb(), which will show the links to the page and its parents in a breadcrumb format.}
14
+ gem.homepage = "https://github.com/sqm/simple_breadcrumbs"
15
+ gem.license = "MIT"
16
+
17
+ gem.files = `git ls-files`.split($/)
18
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
+ gem.require_paths = ['lib']
21
+
22
+ gem.add_dependency 'activerecord', ['>= 3.0', '< 5.0']
23
+ gem.add_dependency 'activesupport', ['>= 3.0', '< 5.0']
24
+
25
+ gem.add_development_dependency 'rspec-rails', '~> 2.13'
26
+ gem.add_development_dependency 'sqlite3', '~> 1.3'
27
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+ require 'has_breadcrumb'
3
+
4
+ class Father
5
+ include HasBreadcrumb
6
+
7
+ has_breadcrumb title: "Father"
8
+ end
9
+
10
+ class Son
11
+ include HasBreadcrumb
12
+
13
+ attr_accessor :father, :name
14
+
15
+ has_breadcrumb parent: :father, title: :name
16
+ end
17
+
18
+ class Daughter
19
+ include HasBreadcrumb
20
+
21
+ attr_reader :father
22
+
23
+ has_breadcrumb parent: :father
24
+ end
25
+
26
+ describe HasBreadcrumb do
27
+ let(:father){ Father.new }
28
+ let(:son){ Son.new }
29
+ let(:daughter) { Daughter.new }
30
+
31
+ describe "#breadcrumb_parent" do
32
+ context "when the parent is nil" do
33
+ it "should return nil" do
34
+ expect(father.breadcrumb_parent).to eq(nil)
35
+ end
36
+ end
37
+
38
+ context "when a parent is given" do
39
+ it "should define a breadcrumb parent" do
40
+ father = Object.new
41
+ son.father = father
42
+ expect(son.breadcrumb_parent).to eq(father)
43
+ end
44
+ end
45
+ end
46
+
47
+ describe "#breadcrumb_name" do
48
+ context "when no title is given" do
49
+ it "should return an empty string" do
50
+ expect(daughter.breadcrumb_name).to eq("")
51
+ end
52
+ end
53
+
54
+ context "when a name is given" do
55
+ it "should return a title when a Symbol is passed" do
56
+ son.name = "Jimmy"
57
+ expect(son.breadcrumb_name).to eq("Jimmy")
58
+ end
59
+
60
+ it "should return a title when a string is passed" do
61
+ expect(father.breadcrumb_name).to eq("Father")
62
+ end
63
+ end
64
+ end
65
+
66
+ describe ".included" do
67
+ it "should respond to has_breadcrumb" do
68
+ Father.should respond_to(:has_breadcrumb)
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ class ObjectWithBreadcrumb
4
+ include ShowBreadcrumb
5
+ end
6
+
7
+ describe ShowBreadcrumb do
8
+ let(:admin) { Admin.create(name: "Admins") }
9
+ let(:manager) { Manager.create(name: "Managers") }
10
+ let(:user) { User.create(name: "Users") }
11
+ let(:controller) { ActionController::Base.new }
12
+
13
+ describe "#collect_crumbs" do
14
+ subject(:object_with_breadcrumb) { ObjectWithBreadcrumb.new }
15
+
16
+ context "when an object has no parent" do
17
+ it "should return an array containing itself" do
18
+ crumbs = object_with_breadcrumb.collect_crumbs(admin, [])
19
+ expect(crumbs).to eq([admin])
20
+ end
21
+ end
22
+
23
+ context "when an object has a parent" do
24
+ it "should return an array with itself and parents" do
25
+ user.manager = manager
26
+ crumbs = object_with_breadcrumb.collect_crumbs(user, [])
27
+ expect(crumbs).to eq([user, manager])
28
+ end
29
+ end
30
+ end
31
+
32
+ describe "#breadcrumbs" do
33
+ before { controller.stub(url_for: "fake_url") }
34
+
35
+ context "when :forced_parent is passed" do
36
+ it "should link to the forced parent" do
37
+ breadcrumb = controller.breadcrumb(user, "Add", forced_parent: admin)
38
+ expect(breadcrumb).to eq("<a href=\"fake_url\">Admin</a> &gt; <a href=\"fake_url\">Users</a> &gt; Add User")
39
+ end
40
+ end
41
+
42
+ context "when there is only one crumb" do
43
+ it "should not return a parent link" do
44
+ breadcrumb = controller.breadcrumb(admin, "Add")
45
+ expect(breadcrumb).to eq("<a href=\"fake_url\">Admins</a> &gt; <a href=\"fake_url\">Admin</a> &gt; Add Admin")
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,36 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require 'rails/all'
9
+ require 'rspec/rails'
10
+ require 'has_breadcrumb'
11
+
12
+ ActiveSupport.on_load(:active_record) do
13
+ include HasBreadcrumb
14
+ end
15
+
16
+ ActiveSupport.on_load(:action_controller) do
17
+ include ActionView::Helpers
18
+ include ShowBreadcrumb
19
+ end
20
+
21
+ I18n.enforce_available_locales = false
22
+
23
+ # Load support files
24
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
25
+
26
+ RSpec.configure do |config|
27
+ config.treat_symbols_as_metadata_keys_with_true_values = true
28
+ config.run_all_when_everything_filtered = true
29
+ config.filter_run :focus
30
+
31
+ # Run specs in random order to surface order dependencies. If you find an
32
+ # order dependency and want to debug it, you can fix the order by providing
33
+ # the seed, which is printed after each run.
34
+ # --seed 1234
35
+ config.order = 'random'
36
+ end
@@ -0,0 +1,4 @@
1
+ ActiveRecord::Base.establish_connection(
2
+ :adapter => 'sqlite3',
3
+ :database => ':memory:'
4
+ )
@@ -0,0 +1,5 @@
1
+ class Admin < ActiveRecord::Base
2
+ has_many :managers
3
+
4
+ has_breadcrumb :title => 'Admin'
5
+ end
@@ -0,0 +1,5 @@
1
+ class Intern < ActiveRecord::Base
2
+ belongs_to :manager
3
+
4
+ has_breadcrumb :parent => :manager
5
+ end
@@ -0,0 +1,6 @@
1
+ class Manager < ActiveRecord::Base
2
+ belongs_to :admin
3
+ has_many :users, :foreign_key => :admin_id
4
+
5
+ has_breadcrumb :parent => :admin, :title => :name
6
+ end
@@ -0,0 +1,5 @@
1
+ class User < ActiveRecord::Base
2
+ belongs_to :manager
3
+
4
+ has_breadcrumb :parent => :manager, :title => :name
5
+ end
@@ -0,0 +1,22 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :admins, :force => true do |t|
5
+ t.string :name
6
+ end
7
+
8
+ create_table :managers, :force => true do |t|
9
+ t.string :name
10
+ t.integer :admin_id
11
+ end
12
+
13
+ create_table :users, :force => true do |t|
14
+ t.string :name
15
+ t.integer :manager_id
16
+ end
17
+
18
+ create_table :interns, :force => true do |t|
19
+ t.string :name
20
+ t.integer :manager_id
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_breadcrumbs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tim Harvey
8
+ - Matt Outten
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ - - <
21
+ - !ruby/object:Gem::Version
22
+ version: '5.0'
23
+ prerelease: false
24
+ name: activerecord
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - - <
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ type: :runtime
34
+ - !ruby/object:Gem::Dependency
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '3.0'
40
+ - - <
41
+ - !ruby/object:Gem::Version
42
+ version: '5.0'
43
+ prerelease: false
44
+ name: activesupport
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '3.0'
50
+ - - <
51
+ - !ruby/object:Gem::Version
52
+ version: '5.0'
53
+ type: :runtime
54
+ - !ruby/object:Gem::Dependency
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ version: '2.13'
60
+ prerelease: false
61
+ name: rspec-rails
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: '2.13'
67
+ type: :development
68
+ - !ruby/object:Gem::Dependency
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ~>
72
+ - !ruby/object:Gem::Version
73
+ version: '1.3'
74
+ prerelease: false
75
+ name: sqlite3
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ~>
79
+ - !ruby/object:Gem::Version
80
+ version: '1.3'
81
+ type: :development
82
+ description: Provides a simple and flexible way to create breadcrumbs with Rails active
83
+ record models.
84
+ email:
85
+ - developers@squaremouth.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE
93
+ - README.md
94
+ - Rakefile
95
+ - lib/has_breadcrumb.rb
96
+ - lib/has_breadcrumb/has_breadcrumb.rb
97
+ - lib/has_breadcrumb/railtie.rb
98
+ - lib/has_breadcrumb/show_breadcrumb.rb
99
+ - lib/has_breadcrumb/version.rb
100
+ - simple_breadcrumbs.gemspec
101
+ - spec/has_breadcrumb/has_breadcrumb_spec.rb
102
+ - spec/has_breadcrumb/show_breadcrumb_spec.rb
103
+ - spec/spec_helper.rb
104
+ - spec/support/adapters/active_record.rb
105
+ - spec/support/models/admin.rb
106
+ - spec/support/models/intern.rb
107
+ - spec/support/models/manager.rb
108
+ - spec/support/models/user.rb
109
+ - spec/support/schema.rb
110
+ homepage: https://github.com/sqm/simple_breadcrumbs
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.2.1
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Setting has_breadcrumb on a model will enable a view method, breadcrumb(),
134
+ which will show the links to the page and its parents in a breadcrumb format.
135
+ test_files:
136
+ - spec/has_breadcrumb/has_breadcrumb_spec.rb
137
+ - spec/has_breadcrumb/show_breadcrumb_spec.rb
138
+ - spec/spec_helper.rb
139
+ - spec/support/adapters/active_record.rb
140
+ - spec/support/models/admin.rb
141
+ - spec/support/models/intern.rb
142
+ - spec/support/models/manager.rb
143
+ - spec/support/models/user.rb
144
+ - spec/support/schema.rb
145
+ has_rdoc: