partial_path_customizer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.travis.yml +8 -0
  4. data/Appraisals +5 -0
  5. data/Gemfile +14 -0
  6. data/Gemfile.lock +90 -0
  7. data/LICENSE +20 -0
  8. data/README.md +76 -0
  9. data/Rakefile +18 -0
  10. data/gemfiles/rails_3.2.gemfile +7 -0
  11. data/gemfiles/rails_4.0.gemfile +7 -0
  12. data/gemfiles/rails_4.1.gemfile +7 -0
  13. data/lib/partial_path_customizer.rb +5 -0
  14. data/lib/partial_path_customizer/partial_path_helper.rb +19 -0
  15. data/lib/partial_path_customizer/presenter.rb +34 -0
  16. data/lib/partial_path_customizer/railtie.rb +9 -0
  17. data/lib/partial_path_customizer/version.rb +3 -0
  18. data/lib/tasks/partial_path_customizer_tasks.rake +4 -0
  19. data/partial_path_customizer.gemspec +29 -0
  20. data/spec/dummy/.rspec +2 -0
  21. data/spec/dummy/README.rdoc +28 -0
  22. data/spec/dummy/Rakefile +6 -0
  23. data/spec/dummy/app/assets/images/.keep +0 -0
  24. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  25. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  26. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  27. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  28. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  29. data/spec/dummy/app/mailers/.keep +0 -0
  30. data/spec/dummy/app/models/.keep +0 -0
  31. data/spec/dummy/app/models/concerns/.keep +0 -0
  32. data/spec/dummy/app/models/dealer.rb +2 -0
  33. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  34. data/spec/dummy/bin/bundle +3 -0
  35. data/spec/dummy/bin/rails +4 -0
  36. data/spec/dummy/bin/rake +4 -0
  37. data/spec/dummy/config.ru +4 -0
  38. data/spec/dummy/config/application.rb +31 -0
  39. data/spec/dummy/config/boot.rb +5 -0
  40. data/spec/dummy/config/database.yml +25 -0
  41. data/spec/dummy/config/environment.rb +5 -0
  42. data/spec/dummy/config/environments/development.rb +37 -0
  43. data/spec/dummy/config/environments/production.rb +78 -0
  44. data/spec/dummy/config/environments/test.rb +39 -0
  45. data/spec/dummy/config/initializers/assets.rb +8 -0
  46. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  47. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  48. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  49. data/spec/dummy/config/initializers/inflections.rb +16 -0
  50. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  51. data/spec/dummy/config/initializers/session_store.rb +3 -0
  52. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  53. data/spec/dummy/config/locales/en.yml +23 -0
  54. data/spec/dummy/config/routes.rb +56 -0
  55. data/spec/dummy/config/secrets.yml +22 -0
  56. data/spec/dummy/db/migrate/20141009025609_create_dealers.rb +9 -0
  57. data/spec/dummy/lib/assets/.keep +0 -0
  58. data/spec/dummy/log/.keep +0 -0
  59. data/spec/dummy/public/404.html +67 -0
  60. data/spec/dummy/public/422.html +67 -0
  61. data/spec/dummy/public/500.html +66 -0
  62. data/spec/dummy/public/favicon.ico +0 -0
  63. data/spec/helpers/partial_path_helper_spec.rb +37 -0
  64. data/spec/partial_path_customizer/presenter_spec.rb +82 -0
  65. data/spec/rails_helper.rb +55 -0
  66. data/spec/spec_helper.rb +85 -0
  67. metadata +240 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 83f2818e752af14f6ccf04ea695cc1f8d4b38a82
4
+ data.tar.gz: 3d253c2b993a5c6f73c87df5447558f63c779b6b
5
+ SHA512:
6
+ metadata.gz: e41f9a48ea948f96e51d2da0601ead47b3f6dc56a4da1da480cc5b0426fa0f69800052ddca796e2cbc2f25053dbadf3de644456cd108021ce18594f7911ca53e
7
+ data.tar.gz: e23ef878c7b944c6108ff6a0009871d6da571f904d7bd17f938b70992842160a9c4b8544e1a049e0326d55d566aa2838036267d32f98d2cce0c6c5b604ce29ca
@@ -0,0 +1,10 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ spec/dummy/db/*.sqlite3
5
+ spec/dummy/db/*.sqlite3-journal
6
+ spec/dummy/log/*.log
7
+ spec/dummy/tmp/
8
+ spec/dummy/.sass-cache
9
+ gemfiles/*.lock
10
+ *.gem
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ gemfile:
6
+ - gemfiles/rails_3.2.gemfile
7
+ - gemfiles/rails_4.0.gemfile
8
+ - gemfiles/rails_4.1.gemfile
@@ -0,0 +1,5 @@
1
+ ['4.1.6', '4.0.10', '3.2.19'].each do |rails_version|
2
+ appraise "rails_#{rails_version[0..2]}" do
3
+ gem "rails", rails_version
4
+ end
5
+ end
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Declare your gem's dependencies in partial_path_customizer.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # Declare any dependencies that are still in development here instead of in
9
+ # your gemspec. These might include edge Rails or gems from your path or
10
+ # Git. Remember to move these dependencies to your gemspec before releasing
11
+ # your gem to rubygems.org.
12
+
13
+ # To use debugger
14
+ # gem 'debugger'
@@ -0,0 +1,90 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ partial_path_customizer (0.1.0)
5
+ dumb_delegator (~> 0.6)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (4.1.6)
11
+ actionview (= 4.1.6)
12
+ activesupport (= 4.1.6)
13
+ rack (~> 1.5.2)
14
+ rack-test (~> 0.6.2)
15
+ actionview (4.1.6)
16
+ activesupport (= 4.1.6)
17
+ builder (~> 3.1)
18
+ erubis (~> 2.7.0)
19
+ activemodel (4.1.6)
20
+ activesupport (= 4.1.6)
21
+ builder (~> 3.1)
22
+ activesupport (4.1.6)
23
+ i18n (~> 0.6, >= 0.6.9)
24
+ json (~> 1.7, >= 1.7.7)
25
+ minitest (~> 5.1)
26
+ thread_safe (~> 0.1)
27
+ tzinfo (~> 1.1)
28
+ appraisal (1.0.2)
29
+ bundler
30
+ rake
31
+ thor (>= 0.14.0)
32
+ builder (3.2.2)
33
+ coderay (1.1.0)
34
+ diff-lcs (1.2.5)
35
+ dumb_delegator (0.7.0)
36
+ erubis (2.7.0)
37
+ i18n (0.6.11)
38
+ json (1.8.1)
39
+ method_source (0.8.2)
40
+ minitest (5.4.2)
41
+ pry (0.10.0)
42
+ coderay (~> 1.1.0)
43
+ method_source (~> 0.8.1)
44
+ slop (~> 3.4)
45
+ rack (1.5.2)
46
+ rack-test (0.6.2)
47
+ rack (>= 1.0)
48
+ railties (4.1.6)
49
+ actionpack (= 4.1.6)
50
+ activesupport (= 4.1.6)
51
+ rake (>= 0.8.7)
52
+ thor (>= 0.18.1, < 2.0)
53
+ rake (10.3.2)
54
+ rspec-activemodel-mocks (1.0.1)
55
+ activemodel (>= 3.0)
56
+ activesupport (>= 3.0)
57
+ rspec-mocks (>= 2.99, < 4.0)
58
+ rspec-core (3.1.5)
59
+ rspec-support (~> 3.1.0)
60
+ rspec-expectations (3.1.2)
61
+ diff-lcs (>= 1.2.0, < 2.0)
62
+ rspec-support (~> 3.1.0)
63
+ rspec-mocks (3.1.2)
64
+ rspec-support (~> 3.1.0)
65
+ rspec-rails (3.1.0)
66
+ actionpack (>= 3.0)
67
+ activesupport (>= 3.0)
68
+ railties (>= 3.0)
69
+ rspec-core (~> 3.1.0)
70
+ rspec-expectations (~> 3.1.0)
71
+ rspec-mocks (~> 3.1.0)
72
+ rspec-support (~> 3.1.0)
73
+ rspec-support (3.1.1)
74
+ slop (3.5.0)
75
+ sqlite3 (1.3.9)
76
+ thor (0.19.1)
77
+ thread_safe (0.3.4)
78
+ tzinfo (1.2.2)
79
+ thread_safe (~> 0.1)
80
+
81
+ PLATFORMS
82
+ ruby
83
+
84
+ DEPENDENCIES
85
+ appraisal
86
+ partial_path_customizer!
87
+ pry
88
+ rspec-activemodel-mocks
89
+ rspec-rails (~> 3.0)
90
+ sqlite3
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Animas Code Labs
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,76 @@
1
+ # Partial Path Customizer
2
+
3
+ [![Build Status](https://travis-ci.org/AnimasCodeLabs/partial_path_customizer.svg?branch=master)](https://travis-ci.org/AnimasCodeLabs/partial_path_customizer)
4
+
5
+ Partial Path Customizer allows you to override `#to_partial_path` for a Rails model at runtime.
6
+
7
+ ## Installation & Upgrading
8
+
9
+ This gem is a railtie and is meant to be used with Rails. It is tested with versions of Rails down to 3.2.
10
+
11
+ Partial Path Customizer is recommended to be run as a gem and included in your Gemfile:
12
+
13
+ ```ruby
14
+ gem 'partial_path_customizer'
15
+ ```
16
+
17
+ ## Helper
18
+
19
+ Partial path customizers provides a helper method to automatically set the partial path for the object
20
+ you want to render. Here is how you would render an object normally.
21
+
22
+ ```erb
23
+ <%= render @bike %>
24
+ ```
25
+
26
+ This would render the `bikes/bike` partial. Now if you want to customize the partial path, use the
27
+ `customize_partial_path` like this.
28
+
29
+ ```erb
30
+ <%= render customize_partial_path(@bike, 'summary') %>
31
+ ```
32
+
33
+ This renders the `bikes/summary` partial instead. The partial path with this helper is generated by the
34
+ object's model_name pluralized.
35
+
36
+ ### Heterogenous collections
37
+
38
+ This is also very useful for rendering heterogenous collections with custom partial paths. For example, if you have
39
+ this collection:
40
+
41
+ ```ruby
42
+ # Somewhere in the controller
43
+ @listings = [Bike.new, Wheelset.new, Bike.new]
44
+ ```
45
+
46
+ and render it like this
47
+
48
+ ```erb
49
+ <%= render @listings %>
50
+ ```
51
+
52
+ it will render the partials `bikes/bike`, `wheelsets/wheels`, and `bikes/bike`. If you would like the partial names
53
+ to to be rendered like `bikes/summary`, `wheelsets/summary`, and `bikes/summary`, you can do this
54
+
55
+ ```erb
56
+ <%= render customize_partial_path(@listings, 'summary') %>
57
+ ```
58
+
59
+ ### Customizing partial_path generation
60
+
61
+ If you need to further customize how the partial path is generated, you can pass a callable object in as the second
62
+ argument. The callable object will receive a reference to the model when it's #call method is called.
63
+
64
+ ```ruby
65
+ <%= render customize_partial_path(@bike, ->(model){ "#{model.class.model_name.singular}/#{model.status}_summary" })
66
+ ```
67
+
68
+ That would allow you to generate a partial path like `bike/sold_summary`.
69
+
70
+ **Note:** This is a low-level API and the higher level `customize_object_partial_path(<object>, <partial_name>)` should
71
+ be used in most cases.
72
+
73
+ ## License
74
+
75
+ Partial Path Customizer is Copyright &copy; 2014 Animas Code Labs. It is free software, and may be redistributed
76
+ under the terms specified in the LICENSE file.
@@ -0,0 +1,18 @@
1
+ require 'bundler/setup'
2
+ require 'bundler/gem_tasks'
3
+ require 'appraisal'
4
+
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default do |t|
9
+ if ENV['BUNDLE_GEMFILE'] =~ /gemfiles/
10
+ exec 'rake spec'
11
+ else
12
+ Rake::Task['appraise'].execute
13
+ end
14
+ end
15
+
16
+ task :appraise => ['appraisal:install'] do |t|
17
+ exec 'rake appraisal'
18
+ end
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "3.2.19"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "4.0.10"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "4.1.6"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,5 @@
1
+ require 'partial_path_customizer/version'
2
+
3
+ module PartialPathCustomizer
4
+ require 'partial_path_customizer/railtie' if defined?(Rails)
5
+ end
@@ -0,0 +1,19 @@
1
+ require 'partial_path_customizer/presenter'
2
+
3
+ module PartialPathCustomizer::PartialPathHelper
4
+
5
+ def customize_partial_path object, partial_name_or_callable
6
+ partial_path_callable = proc{|model| "#{model.class.model_name.plural}/#{partial_name_or_callable}" }
7
+
8
+ if partial_name_or_callable.respond_to?(:call)
9
+ partial_path_callable = partial_name_or_callable
10
+ end
11
+
12
+ if object.respond_to?(:map)
13
+ PartialPathCustomizer::Presenter.wrap_collection(object, &partial_path_callable)
14
+ else
15
+ PartialPathCustomizer::Presenter.new(object, &partial_path_callable)
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,34 @@
1
+ require 'dumb_delegator'
2
+
3
+ class PartialPathCustomizer::Presenter < DumbDelegator
4
+
5
+ def self.wrap_collection collection, &partial_path_block
6
+ collection.map do |obj|
7
+ new obj, &partial_path_block
8
+ end
9
+ end
10
+
11
+ def initialize(object, &partial_path_block)
12
+ super(object)
13
+ @partial_path_block = partial_path_block
14
+ end
15
+
16
+ def to_partial_path
17
+ if @partial_path_block.present?
18
+ @partial_path_block.call(_model)
19
+ else
20
+ __getobj__.to_partial_path
21
+ end
22
+ end
23
+
24
+ def to_model
25
+ self
26
+ end
27
+
28
+ private
29
+
30
+ def _model
31
+ __getobj__
32
+ end
33
+
34
+ end
@@ -0,0 +1,9 @@
1
+ require 'partial_path_customizer/partial_path_helper'
2
+
3
+ module PartialPathCustomizer
4
+ class Railtie < Rails::Railtie
5
+ initializer 'partial_path_customizer.helpers' do |app|
6
+ ActionView::Base.send :include, PartialPathCustomizer::PartialPathHelper
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module PartialPathCustomizer
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :partial_path_customizer do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,29 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ # Maintain your gem's version:
4
+ require "partial_path_customizer/version"
5
+
6
+ # Describe your gem and declare its dependencies:
7
+ Gem::Specification.new do |s|
8
+ s.name = "partial_path_customizer"
9
+ s.version = PartialPathCustomizer::VERSION
10
+ s.authors = ["Aaron Renner"]
11
+ s.email = ["aaron@animascodelabs.com"]
12
+ s.homepage = "https://github.com/AnimasCodeLabs/partial_path_customizer"
13
+ s.summary = "Easily override #to_partial_path on Rails models"
14
+ s.description = "Easily override #to_partial_path on Rails models"
15
+ s.license = "MIT"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ['lib']
21
+
22
+ s.add_dependency 'dumb_delegator', '~> 0.6'
23
+
24
+ s.add_development_dependency "appraisal"
25
+ s.add_development_dependency "sqlite3"
26
+ s.add_development_dependency "rspec-rails", "~> 3.0"
27
+ s.add_development_dependency "rspec-activemodel-mocks"
28
+ s.add_development_dependency "pry"
29
+ end
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
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
File without changes