friendly_id-method_scopes 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +2 -0
  3. data/.ruby-gemspec +1 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +3 -0
  6. data/Gemfile +20 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.rdoc +29 -0
  9. data/Rakefile +39 -0
  10. data/VERSION +1 -0
  11. data/coverage/.last_run.json +5 -0
  12. data/coverage/.resultset.json +676 -0
  13. data/coverage/.resultset.json.lock +0 -0
  14. data/friendly_id-method_scopes.gemspec +154 -0
  15. data/lib/friendly_id/method_scopes.rb +69 -0
  16. data/lib/friendly_id/method_scopes/version.rb +9 -0
  17. data/spec/controllers/authors_controller_spec.rb +20 -0
  18. data/spec/controllers/books_controller_spec.rb +20 -0
  19. data/spec/dummy/Rakefile +9 -0
  20. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  21. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  22. data/spec/dummy/app/assets/stylesheets/scaffold.css +1 -0
  23. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  24. data/spec/dummy/app/controllers/authors_controller.rb +18 -0
  25. data/spec/dummy/app/controllers/books_controller.rb +23 -0
  26. data/spec/dummy/app/models/.keep +0 -0
  27. data/spec/dummy/app/models/author.rb +12 -0
  28. data/spec/dummy/app/models/book.rb +16 -0
  29. data/spec/dummy/app/models/role.rb +10 -0
  30. data/spec/dummy/app/views/authors/index.html.haml +5 -0
  31. data/spec/dummy/app/views/authors/show.html.haml +5 -0
  32. data/spec/dummy/app/views/books/index.html.haml +5 -0
  33. data/spec/dummy/app/views/books/show.html.haml +5 -0
  34. data/spec/dummy/app/views/layouts/application.html.haml +10 -0
  35. data/spec/dummy/bin/bundle +3 -0
  36. data/spec/dummy/bin/rails +4 -0
  37. data/spec/dummy/bin/rake +4 -0
  38. data/spec/dummy/bin/setup +29 -0
  39. data/spec/dummy/config.ru +4 -0
  40. data/spec/dummy/config/application.rb +44 -0
  41. data/spec/dummy/config/boot.rb +5 -0
  42. data/spec/dummy/config/database.yml +25 -0
  43. data/spec/dummy/config/environment.rb +5 -0
  44. data/spec/dummy/config/environments/development.rb +41 -0
  45. data/spec/dummy/config/environments/production.rb +79 -0
  46. data/spec/dummy/config/environments/test.rb +42 -0
  47. data/spec/dummy/config/initializers/assets.rb +11 -0
  48. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  49. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  50. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  51. data/spec/dummy/config/initializers/inflections.rb +16 -0
  52. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  53. data/spec/dummy/config/initializers/rolify.rb +7 -0
  54. data/spec/dummy/config/initializers/session_store.rb +3 -0
  55. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  56. data/spec/dummy/config/locales/en.yml +23 -0
  57. data/spec/dummy/config/routes.rb +6 -0
  58. data/spec/dummy/config/secrets.yml +22 -0
  59. data/spec/dummy/db/migrate/20150621221655_create_books.rb +10 -0
  60. data/spec/dummy/db/migrate/20150621221732_create_authors.rb +10 -0
  61. data/spec/dummy/db/migrate/20150622182214_rolify_create_roles.rb +19 -0
  62. data/spec/dummy/db/schema.rb +48 -0
  63. data/spec/dummy/db/seeds.rb +0 -0
  64. data/spec/dummy/db/seeds/authors.seeds.rb +2 -0
  65. data/spec/dummy/db/seeds/books.seeds.rb +9 -0
  66. data/spec/dummy/lib/assets/.keep +0 -0
  67. data/spec/dummy/log/.keep +0 -0
  68. data/spec/dummy/public/404.html +67 -0
  69. data/spec/dummy/public/422.html +67 -0
  70. data/spec/dummy/public/500.html +66 -0
  71. data/spec/dummy/public/favicon.ico +0 -0
  72. data/spec/factories/author.rb +6 -0
  73. data/spec/factories/book.rb +15 -0
  74. data/spec/models/author_spec.rb +11 -0
  75. data/spec/models/book_spec.rb +15 -0
  76. data/spec/rails_helper.rb +45 -0
  77. data/spec/routing/authors_routing_spec.rb +15 -0
  78. data/spec/routing/books_routing_spec.rb +15 -0
  79. data/spec/spec_helper.rb +94 -0
  80. data/spec/support/authors.rb +4 -0
  81. data/spec/support/books_with_authors.rb +7 -0
  82. metadata +306 -0
File without changes
@@ -0,0 +1,154 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: friendly_id-method_scopes 0.1.5 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "friendly_id-method_scopes"
9
+ s.version = "0.1.5"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
13
+ s.authors = ["Karen Lundgren"]
14
+ s.date = "2015-06-22"
15
+ s.description = "FriendlyId Extension to allow method scopes, in addition to column and relationship scopes."
16
+ s.email = "karen.e.lundgren@gmail.com"
17
+ s.extra_rdoc_files = [
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".rspec",
22
+ ".ruby-gemspec",
23
+ ".ruby-version",
24
+ ".travis.yml",
25
+ "Gemfile",
26
+ "MIT-LICENSE",
27
+ "README.rdoc",
28
+ "Rakefile",
29
+ "VERSION",
30
+ "coverage/.last_run.json",
31
+ "coverage/.resultset.json",
32
+ "coverage/.resultset.json.lock",
33
+ "friendly_id-method_scopes.gemspec",
34
+ "lib/friendly_id/method_scopes.rb",
35
+ "lib/friendly_id/method_scopes/version.rb",
36
+ "spec/controllers/authors_controller_spec.rb",
37
+ "spec/controllers/books_controller_spec.rb",
38
+ "spec/dummy/Rakefile",
39
+ "spec/dummy/app/assets/javascripts/application.js",
40
+ "spec/dummy/app/assets/stylesheets/application.css",
41
+ "spec/dummy/app/assets/stylesheets/scaffold.css",
42
+ "spec/dummy/app/controllers/application_controller.rb",
43
+ "spec/dummy/app/controllers/authors_controller.rb",
44
+ "spec/dummy/app/controllers/books_controller.rb",
45
+ "spec/dummy/app/models/.keep",
46
+ "spec/dummy/app/models/author.rb",
47
+ "spec/dummy/app/models/book.rb",
48
+ "spec/dummy/app/models/role.rb",
49
+ "spec/dummy/app/views/authors/index.html.haml",
50
+ "spec/dummy/app/views/authors/show.html.haml",
51
+ "spec/dummy/app/views/books/index.html.haml",
52
+ "spec/dummy/app/views/books/show.html.haml",
53
+ "spec/dummy/app/views/layouts/application.html.haml",
54
+ "spec/dummy/bin/bundle",
55
+ "spec/dummy/bin/rails",
56
+ "spec/dummy/bin/rake",
57
+ "spec/dummy/bin/setup",
58
+ "spec/dummy/config.ru",
59
+ "spec/dummy/config/application.rb",
60
+ "spec/dummy/config/boot.rb",
61
+ "spec/dummy/config/database.yml",
62
+ "spec/dummy/config/environment.rb",
63
+ "spec/dummy/config/environments/development.rb",
64
+ "spec/dummy/config/environments/production.rb",
65
+ "spec/dummy/config/environments/test.rb",
66
+ "spec/dummy/config/initializers/assets.rb",
67
+ "spec/dummy/config/initializers/backtrace_silencers.rb",
68
+ "spec/dummy/config/initializers/cookies_serializer.rb",
69
+ "spec/dummy/config/initializers/filter_parameter_logging.rb",
70
+ "spec/dummy/config/initializers/inflections.rb",
71
+ "spec/dummy/config/initializers/mime_types.rb",
72
+ "spec/dummy/config/initializers/rolify.rb",
73
+ "spec/dummy/config/initializers/session_store.rb",
74
+ "spec/dummy/config/initializers/wrap_parameters.rb",
75
+ "spec/dummy/config/locales/en.yml",
76
+ "spec/dummy/config/routes.rb",
77
+ "spec/dummy/config/secrets.yml",
78
+ "spec/dummy/db/migrate/20150621221655_create_books.rb",
79
+ "spec/dummy/db/migrate/20150621221732_create_authors.rb",
80
+ "spec/dummy/db/migrate/20150622182214_rolify_create_roles.rb",
81
+ "spec/dummy/db/schema.rb",
82
+ "spec/dummy/db/seeds.rb",
83
+ "spec/dummy/db/seeds/authors.seeds.rb",
84
+ "spec/dummy/db/seeds/books.seeds.rb",
85
+ "spec/dummy/lib/assets/.keep",
86
+ "spec/dummy/log/.keep",
87
+ "spec/dummy/public/404.html",
88
+ "spec/dummy/public/422.html",
89
+ "spec/dummy/public/500.html",
90
+ "spec/dummy/public/favicon.ico",
91
+ "spec/factories/author.rb",
92
+ "spec/factories/book.rb",
93
+ "spec/models/author_spec.rb",
94
+ "spec/models/book_spec.rb",
95
+ "spec/rails_helper.rb",
96
+ "spec/routing/authors_routing_spec.rb",
97
+ "spec/routing/books_routing_spec.rb",
98
+ "spec/spec_helper.rb",
99
+ "spec/support/authors.rb",
100
+ "spec/support/books_with_authors.rb"
101
+ ]
102
+ s.homepage = "http://www.gemvein.com/museum/cases/friendly_id-method_scopes"
103
+ s.rubygems_version = "2.4.5"
104
+ s.summary = "FriendlyId Extension to allow method scopes."
105
+
106
+ if s.respond_to? :specification_version then
107
+ s.specification_version = 4
108
+
109
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
110
+ s.add_runtime_dependency(%q<rails>, ["~> 4"])
111
+ s.add_runtime_dependency(%q<friendly_id>, ["~> 5"])
112
+ s.add_runtime_dependency(%q<jeweler>, ["~> 2"])
113
+ s.add_runtime_dependency(%q<coveralls>, ["~> 0.8"])
114
+ s.add_development_dependency(%q<sqlite3>, ["~> 1"])
115
+ s.add_development_dependency(%q<rspec-rails>, ["~> 3.3"])
116
+ s.add_development_dependency(%q<shoulda-matchers>, ["~> 2.8"])
117
+ s.add_development_dependency(%q<rolify>, ["~> 4"])
118
+ s.add_development_dependency(%q<seedbank>, ["~> 0.3"])
119
+ s.add_development_dependency(%q<factory_girl_rails>, ["~> 4.5"])
120
+ s.add_development_dependency(%q<faker>, ["~> 1.4"])
121
+ s.add_development_dependency(%q<haml>, ["~> 4"])
122
+ s.add_development_dependency(%q<database_cleaner>, ["~> 1.4"])
123
+ else
124
+ s.add_dependency(%q<rails>, ["~> 4"])
125
+ s.add_dependency(%q<friendly_id>, ["~> 5"])
126
+ s.add_dependency(%q<jeweler>, ["~> 2"])
127
+ s.add_dependency(%q<coveralls>, ["~> 0.8"])
128
+ s.add_dependency(%q<sqlite3>, ["~> 1"])
129
+ s.add_dependency(%q<rspec-rails>, ["~> 3.3"])
130
+ s.add_dependency(%q<shoulda-matchers>, ["~> 2.8"])
131
+ s.add_dependency(%q<rolify>, ["~> 4"])
132
+ s.add_dependency(%q<seedbank>, ["~> 0.3"])
133
+ s.add_dependency(%q<factory_girl_rails>, ["~> 4.5"])
134
+ s.add_dependency(%q<faker>, ["~> 1.4"])
135
+ s.add_dependency(%q<haml>, ["~> 4"])
136
+ s.add_dependency(%q<database_cleaner>, ["~> 1.4"])
137
+ end
138
+ else
139
+ s.add_dependency(%q<rails>, ["~> 4"])
140
+ s.add_dependency(%q<friendly_id>, ["~> 5"])
141
+ s.add_dependency(%q<jeweler>, ["~> 2"])
142
+ s.add_dependency(%q<coveralls>, ["~> 0.8"])
143
+ s.add_dependency(%q<sqlite3>, ["~> 1"])
144
+ s.add_dependency(%q<rspec-rails>, ["~> 3.3"])
145
+ s.add_dependency(%q<shoulda-matchers>, ["~> 2.8"])
146
+ s.add_dependency(%q<rolify>, ["~> 4"])
147
+ s.add_dependency(%q<seedbank>, ["~> 0.3"])
148
+ s.add_dependency(%q<factory_girl_rails>, ["~> 4.5"])
149
+ s.add_dependency(%q<faker>, ["~> 1.4"])
150
+ s.add_dependency(%q<haml>, ["~> 4"])
151
+ s.add_dependency(%q<database_cleaner>, ["~> 1.4"])
152
+ end
153
+ end
154
+
@@ -0,0 +1,69 @@
1
+ module FriendlyId
2
+ module MethodScopes
3
+
4
+ # FriendlyId::Config.use will invoke this method when present, to allow
5
+ # loading dependent modules prior to overriding them when necessary.
6
+ def self.setup(model_class)
7
+ model_class.friendly_id_config.use :slugged
8
+ end
9
+
10
+ # Sets up behavior and configuration options for FriendlyId::MethodScopes's method_scopes slugs
11
+ # feature.
12
+ def self.included(model_class)
13
+ model_class.class_eval do
14
+ friendly_id_config.class.send :include, Configuration
15
+ end
16
+ end
17
+
18
+ # # This doesn't work yet, but will be used to hook up to the history module
19
+ # def serialized_scope
20
+ # friendly_id_config.scope_method_columns.sort.map { |column, method| "#{column}:#{send(method)}" }.join(",")
21
+ # end
22
+
23
+ def scope_for_slug_generator
24
+ relation = self.class.unscoped.friendly
25
+ friendly_id_config.scope_method_columns.each do |column, method|
26
+ relation = relation.where(column => send(method))
27
+ end
28
+ primary_key_name = self.class.primary_key
29
+ relation.where(self.class.arel_table[primary_key_name].not_eq(send(primary_key_name)))
30
+ end
31
+ private :scope_for_slug_generator
32
+
33
+ def slug_generator
34
+ friendly_id_config.slug_generator_class.new(scope_for_slug_generator)
35
+ end
36
+ private :slug_generator
37
+
38
+ module Configuration
39
+ attr_accessor :scope_methods
40
+
41
+ def scope_method_columns
42
+ [@scope_methods].flatten.map { |s|
43
+ column = (method_foreign_key(s) or method_so_id(s) or s).to_s
44
+ my_method = (method_foreign_key(s) or method_so_method(s) or s).to_s
45
+ [column, my_method]
46
+ }
47
+ end
48
+
49
+ private
50
+
51
+ def method_so_id(scope)
52
+ if model_class.method_defined? scope
53
+ :id
54
+ end
55
+ end
56
+
57
+ def method_so_method(scope)
58
+ if model_class.method_defined? scope
59
+ scope
60
+ end
61
+ end
62
+
63
+ def method_foreign_key(scope)
64
+ reflection = model_class.reflections[scope] || model_class.reflections[scope.to_s]
65
+ reflection.try(:foreign_key)
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,9 @@
1
+ module FriendlyId
2
+ module MethodScopes
3
+ VERSION = File.read(File.expand_path('../../../../VERSION', __FILE__))
4
+
5
+ def self.version_string
6
+ "FriendlyId::MethodScopes version #{FriendlyId::MethodScopes::VERSION}"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe AuthorsController do
4
+ include_context 'books with authors'
5
+
6
+ describe "GET #index" do
7
+ it "assigns all authors as @authors" do
8
+ get :index
9
+ expect(assigns(:authors)).to eq([first_author, second_author])
10
+ end
11
+ end
12
+
13
+ describe "GET #show" do
14
+ it "assigns the requested author as @author" do
15
+ get :show, {:id => first_author.to_param}
16
+ expect(assigns(:author)).to eq(first_author)
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,20 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe BooksController do
4
+ include_context 'books with authors'
5
+
6
+ describe "GET #index" do
7
+ it "assigns author's books as @books" do
8
+ get :index, author_id: first_author.to_param
9
+ expect(assigns(:books)).to eq([book_a, book_b_first])
10
+ end
11
+ end
12
+
13
+ describe "GET #show" do
14
+ it "assigns the requested book as @book" do
15
+ get :show, author_id: first_author.to_param, id: book_b_first.to_param
16
+ expect(assigns(:book)).to eq(book_b_first)
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,9 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
7
+
8
+ require 'seedbank'
9
+ Seedbank.load_tasks if defined?(Seedbank)
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1 @@
1
+ /* I hate this file. */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,18 @@
1
+ class AuthorsController < ApplicationController
2
+ before_action :set_author, only: :show
3
+
4
+ # GET /authors
5
+ def index
6
+ @authors = Author.all
7
+ end
8
+
9
+ # GET /authors/author-slug
10
+ def show
11
+ end
12
+
13
+ private
14
+ # Use callbacks to share common setup or constraints between actions.
15
+ def set_author
16
+ @author = Author.friendly.find(params[:id])
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ class BooksController < ApplicationController
2
+ before_action :set_author
3
+ before_action :set_book, only: :show
4
+
5
+ # GET /authors/author-slug/books
6
+ def index
7
+ @books = @author.books
8
+ end
9
+
10
+ # GET /authors/author-slug/books/book-slug
11
+ def show
12
+ end
13
+
14
+ private
15
+ # Use callbacks to share common setup or constraints between actions.
16
+ def set_book
17
+ @book = @author.books.friendly.find(params[:id])
18
+ end
19
+
20
+ def set_author
21
+ @author = Author.friendly.find(params[:author_id])
22
+ end
23
+ end
File without changes
@@ -0,0 +1,12 @@
1
+ class Author < ActiveRecord::Base
2
+ # Rolify Gem
3
+ rolify
4
+
5
+ # FriendlyId Gem
6
+ extend FriendlyId
7
+ friendly_id :full_name, use: :slugged
8
+
9
+ def books
10
+ Book.with_role(:author, self)
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ class Book < ActiveRecord::Base
2
+ # Rolify Gem
3
+ resourcify
4
+
5
+ # FriendlyId Gem
6
+ extend FriendlyId
7
+ friendly_id :title, use: [:slugged, :method_scopes], scope_methods: :also_written_by
8
+
9
+ def authors
10
+ Author.with_role(:author, self)
11
+ end
12
+
13
+ def also_written_by
14
+ Book.where(id: authors.collect { |o| o.books.ids }.flatten )
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ class Role < ActiveRecord::Base
2
+ has_and_belongs_to_many :authors, :join_table => :authors_roles
3
+ belongs_to :resource, :polymorphic => true
4
+
5
+ validates :resource_type,
6
+ :inclusion => { :in => Rolify.resource_types },
7
+ :allow_nil => true
8
+
9
+ scopify
10
+ end
@@ -0,0 +1,5 @@
1
+ %p#notice= notice
2
+ %h1 Listing Authors
3
+ %ul
4
+ - @authors.each do |author|
5
+ %li= link_to author.full_name, author
@@ -0,0 +1,5 @@
1
+ %p#notice= notice
2
+ %h1
3
+ = @author.full_name
4
+ %p
5
+ = link_to 'Books', author_books_path(@author)
@@ -0,0 +1,5 @@
1
+ %p#notice= notice
2
+ %h1= "Listing Books by #{@author.full_name}"
3
+ %ul
4
+ - @books.each do |book|
5
+ %li= link_to book.title, [@author, book]