dry_views 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/.gitignore +75 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +8 -0
  4. data/CHANGELOG +2 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE +22 -0
  7. data/README.md +134 -0
  8. data/Rakefile +29 -0
  9. data/dry_views.gemspec +25 -0
  10. data/lib/dry_views.rb +12 -0
  11. data/lib/dry_views/rails3_two.rb +70 -0
  12. data/lib/dry_views/rails3_zero.rb +48 -0
  13. data/lib/dry_views/railtie.rb +23 -0
  14. data/lib/dry_views/version.rb +3 -0
  15. data/spec/controllers/dummy_controller_spec.rb +25 -0
  16. data/spec/dummy/Rakefile +7 -0
  17. data/spec/dummy/app/assets/images/rails.png +0 -0
  18. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  19. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  20. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  21. data/spec/dummy/app/controllers/dummy_controller.rb +6 -0
  22. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  23. data/spec/dummy/app/mailers/.gitkeep +0 -0
  24. data/spec/dummy/app/models/.gitkeep +0 -0
  25. data/spec/dummy/app/views/dummy/index.html.erb +5 -0
  26. data/spec/dummy/app/views/layouts/application.html.erb +24 -0
  27. data/spec/dummy/config.ru +4 -0
  28. data/spec/dummy/config/application.rb +65 -0
  29. data/spec/dummy/config/boot.rb +6 -0
  30. data/spec/dummy/config/database.yml +25 -0
  31. data/spec/dummy/config/environment.rb +5 -0
  32. data/spec/dummy/config/environments/development.rb +37 -0
  33. data/spec/dummy/config/environments/test.rb +37 -0
  34. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  35. data/spec/dummy/config/initializers/inflections.rb +15 -0
  36. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  37. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  38. data/spec/dummy/config/initializers/session_store.rb +8 -0
  39. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  40. data/spec/dummy/config/locales/en.yml +5 -0
  41. data/spec/dummy/config/routes.rb +7 -0
  42. data/spec/dummy/lib/assets/.gitkeep +0 -0
  43. data/spec/dummy/lib/tasks/.gitkeep +0 -0
  44. data/spec/dummy/log/.gitkeep +0 -0
  45. data/spec/dummy/public/404.html +26 -0
  46. data/spec/dummy/public/422.html +26 -0
  47. data/spec/dummy/public/500.html +25 -0
  48. data/spec/dummy/public/favicon.ico +0 -0
  49. data/spec/dummy/public/index.html +241 -0
  50. data/spec/dummy/public/robots.txt +5 -0
  51. data/spec/dummy/script/rails +6 -0
  52. data/spec/spec_helper.rb +25 -0
  53. metadata +216 -0
data/.gitignore ADDED
@@ -0,0 +1,75 @@
1
+ # gem generated
2
+ *.gem
3
+ Gemfile.lock
4
+
5
+ # bundle generated
6
+ .bundle
7
+ lib/bundler/man
8
+
9
+ # rcov generated
10
+ coverage
11
+
12
+ # rdoc generated
13
+ rdoc
14
+
15
+ # yard generated
16
+ doc
17
+ .yardoc
18
+
19
+ # bundler
20
+ .bundle
21
+
22
+ # jeweler generated
23
+ pkg
24
+
25
+ # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
26
+ #
27
+ # * Create a file at ~/.gitignore
28
+ # * Include files you want ignored
29
+ # * Run: git config --global core.excludesfile ~/.gitignore
30
+ #
31
+ # After doing this, these files will be ignored in all your git projects,
32
+ # saving you from having to 'pollute' every project you touch with them
33
+ #
34
+ # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
35
+ #
36
+ # For MacOS:
37
+ #
38
+ .DS_Store
39
+
40
+ # For TextMate
41
+ *.tmproj
42
+ tmtags
43
+
44
+ # For RubyMine
45
+ .idea
46
+
47
+ # For emacs:
48
+ *~
49
+ \#*
50
+ .\#*
51
+
52
+ # For vim:
53
+ *.swp
54
+
55
+ # For redcar:
56
+ .redcar
57
+
58
+ # For rubinius:
59
+ *.rbc
60
+
61
+ # From bundle gem my_gem's .gitignore (good practice?)
62
+ .config
63
+ InstalledFiles
64
+ doc/
65
+ spec/reports
66
+ test/tmp
67
+ test/version_tmp
68
+ tmp
69
+
70
+ # Other
71
+ log/*.log
72
+ spec/dummy/db/*.sqlite3
73
+ spec/dummy/log/*.log
74
+ spec/dummy/tmp/
75
+ spec/dummy/.sass-cache
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - jruby-19mode
6
+ - rbx-19mode
7
+ - ruby-head
8
+ - jruby-head
data/CHANGELOG ADDED
@@ -0,0 +1,2 @@
1
+ Version 0.0.1 - AUG.14.2012
2
+ - Initial Release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dry_views.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Peter Boling
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,134 @@
1
+ # DryViews
2
+
3
+ Keep the views dry with content_for_with_default and friends!
4
+
5
+ DryViews provides extensions to ActionView::Helpers::CaptureHelper, which is part of ActionPack:
6
+ * content_for_with_default
7
+ * no_content_for
8
+ * content_for (enhanced to play nice with the above methods)
9
+
10
+ The rails rendering chain is sort of inside out so the template rendering happens first, and this is what makes it possible to override everything! The template will ALWAYS override the layout, and a partial will always override a partial layout.
11
+
12
+ Here are some guidelines for using the code below:
13
+
14
+ - no_content_for :key
15
+ # Use a dash (-) not equals (=)
16
+ # Will prevent a later content_for from rendering. This allows template overrides of layouts.
17
+
18
+ = content_for_with_default :key
19
+ # Use an equal (=) not a dash (-)
20
+ # You provide it with the default HAML via a block *or* a set of params that are the same as you would pass to a standard "render" call (i.e. :partial => 'foo', :locals => {:bar => 'bar'}).
21
+
22
+ = content_for :key
23
+ # Use an equal (=) not a dash (-)
24
+ # You provide it with a block, i.e. {render :partial => 'foo'}, and it will override content_for_with_default. It has the same precedence as no_content_for, so whichever is rendered first wins, so if a layout has either no_content_for or content_for (with or without default) the template can now override it.
25
+
26
+ ## Installation
27
+
28
+ Add this line to your application's Gemfile:
29
+
30
+ gem 'dry_views'
31
+
32
+ And then execute:
33
+
34
+ $ bundle
35
+
36
+ Or install it yourself as:
37
+
38
+ $ gem install dry_views
39
+
40
+ ## Usage
41
+
42
+ Example #1:
43
+ in a layout
44
+
45
+ - no_content_for :breakfast # ARROWED!
46
+
47
+ in a template
48
+
49
+ = content_for :breakfast do # WINNER!
50
+ %h1 Dub Step
51
+
52
+ will have Dub Step
53
+
54
+ Example #2:
55
+ in a layout
56
+
57
+ = content_for :breakfast do # ARROWED!
58
+ %h1 Dub Step
59
+
60
+ in a template
61
+
62
+ - no_content_for :breakfast # WINNER!
63
+
64
+ will have NOTHING
65
+
66
+ Example #3:
67
+
68
+ in a layout
69
+
70
+ = content_for :breakfast do # WINNER!
71
+ %h1 Dub Step
72
+
73
+ in a template
74
+
75
+ = content_for :breakfast do # WINNER!
76
+ %h1 Metal
77
+
78
+ will have Dub Step and Metal (Concatenation!)
79
+
80
+ Example #4:
81
+ in a layout
82
+
83
+ = content_for_with_default :breakfast do # ARROWED!
84
+ %h1 Dub Step
85
+
86
+ in a template
87
+
88
+ = content_for :breakfast do # WINNER!
89
+ %h1 Metal
90
+
91
+ will have Metal only (No Dub Step!)
92
+
93
+ Example #5:
94
+
95
+ in a layout
96
+
97
+ = content_for_with_default :breakfast do # ARROWED!
98
+ %h1 Dub Step
99
+
100
+ in a template
101
+
102
+ - no_content_for :breakfast # WINNER!
103
+
104
+ will have NOTHING
105
+
106
+ Example #6:
107
+ in a layout
108
+
109
+ = content_for_with_default :breakfast do # ARROWED!
110
+ %h1 Dub Step
111
+
112
+ in a template
113
+
114
+ = render :partial => 'hip_hop', :layout => 'sample'
115
+ = no_content_for :breakfast do # ARROWED!
116
+
117
+ in the 'sample' partial layout
118
+
119
+ = no_content_for :breakfast do # ARROWED!
120
+
121
+ in the 'hip_hop' partial
122
+
123
+ = content_for :breakfast do # WINNER!
124
+ %h1 Hip Hop
125
+
126
+ will have Hip Hop only (No Dub Step!)
127
+
128
+ ## Contributing
129
+
130
+ 1. Fork it
131
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
132
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
133
+ 4. Push to the branch (`git push origin my-new-feature`)
134
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake'
5
+
6
+ require 'rspec/core'
7
+ require 'rspec/core/rake_task'
8
+ RSpec::Core::RakeTask.new(:spec) do |spec|
9
+ spec.pattern = FileList['spec/**/*_spec.rb']
10
+ end
11
+
12
+ require 'reek/rake/task'
13
+ Reek::Rake::Task.new do |t|
14
+ t.fail_on_error = true
15
+ t.verbose = false
16
+ t.source_files = 'lib/**/*.rb'
17
+ end
18
+
19
+ require 'roodi'
20
+ require 'roodi_task'
21
+ RoodiTask.new do |t|
22
+ t.verbose = false
23
+ end
24
+
25
+ task :default => :spec
26
+
27
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
28
+
29
+ Bundler::GemHelper.install_tasks
data/dry_views.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/dry_views/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Peter Boling"]
6
+ gem.email = ["peter.boling@gmail.com"]
7
+ gem.description = %q{Provides extensions to ActionView::Helpers::CaptureHelper: content_for_with_default, no_content_for, and an enhanced content_for that plays nice with friends}
8
+ gem.summary = %q{Keep the views dry with content_for_with_default and friends!}
9
+ gem.homepage = "https://github.com/pboling/dry_views"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "dry_views"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = DryViews::VERSION
17
+
18
+ # Development Dependencies
19
+ gem.add_development_dependency(%q<rails>, ["> 3"])
20
+ gem.add_development_dependency(%q<activesupport>, ["> 3"])
21
+ gem.add_development_dependency(%q<rspec-rails>, [">= 2.8.0"])
22
+ gem.add_development_dependency(%q<reek>, [">= 1.2.8"])
23
+ gem.add_development_dependency(%q<roodi>, [">= 2.1.0"])
24
+
25
+ end
data/lib/dry_views.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "dry_views/version"
2
+
3
+ module DryViews
4
+
5
+ EMPTY_CONTENT = ' ' # For Rails 3.0+ only works with ' '.
6
+ # nil, false, :empty, cannot work, because @view_flow.get(name) is always converted to an
7
+ # ActiveSupport::SafeBuffer (a fancy string)
8
+
9
+ require 'dry_views/railtie'
10
+
11
+
12
+ end
@@ -0,0 +1,70 @@
1
+ module ActionView
2
+ module Helpers
3
+ module CaptureHelper
4
+
5
+ # Reference: http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html
6
+ #
7
+ # Custom method (not overriding anything in Rails)
8
+ #
9
+ # In a template/layout
10
+ #
11
+ # = content_for_with_default :my_default_content, :partial => 'layouts/my_default_content'
12
+ #
13
+ # Override in a nested layout/template/partial:
14
+ #
15
+ # = content_for :my_default_content do
16
+ # = render :partial => 'layouts/my_custom_content', :collection => my_stuff, :as => :stuff
17
+ #
18
+ # All params after the first will be used as options for the default render
19
+ #
20
+ # = content_for_with_default :my_default_content, :partial => 'some/partial', :locals => {:variable => 'Some Text'}
21
+ #
22
+ def content_for_with_default(name, *args, &block)
23
+ if content_check = content_for?(name) # returns true, false, or :empty
24
+ content_for_without_empty_check(name) unless content_check == :empty # when content_check is :empty, does nothing
25
+ elsif block_given?
26
+ content_for_without_empty_check(name, yield) # No need to recheck for empty here, as initial condition handles the :empty case
27
+ else
28
+ options = args.extract_options!
29
+ # Supports the default API of with content as the second param, content_for(name, content = nil, &block)
30
+ if options.empty?
31
+ content_for_without_empty_check(name, args.first) # No need to recheck for empty here, as initial condition handles the :empty case
32
+ else
33
+ render options
34
+ end
35
+ end
36
+ end
37
+
38
+ # Override file actionpack/lib/action_view/helpers/capture_helper.rb, line 136
39
+ # If the empty check has already been performed (as content_for_with_default), you can call content_for_without_empty_check to bypass a duplicate check
40
+ def content_for_with_empty_check(name, content = nil, &block)
41
+ if content_for?(name) == :empty
42
+ # Some preceding layout or template has already specified that there is to be no content for this name, and that is FINAL.
43
+ return nil
44
+ else
45
+ content_for_without_empty_check(name, content, &block)
46
+ end
47
+ end
48
+ alias_method_chain :content_for, :empty_check
49
+
50
+ # Override file actionpack/lib/action_view/helpers/capture_helper.rb, line 175
51
+ def content_for_with_empty_check?(name)
52
+ if @view_flow.get(name) == DryViews::EMPTY_CONTENT
53
+ #add a check for empty, and return :empty, so we can switch on it for empty content
54
+ return :empty
55
+ elsif content_for_without_empty_check?(name)
56
+ return true
57
+ else
58
+ return false
59
+ end
60
+ end
61
+ alias_method_chain :content_for?, :empty_check
62
+
63
+ # Custom method (not overriding anything in Rails)
64
+ def no_content_for(name)
65
+ @view_flow.append!(name, DryViews::EMPTY_CONTENT)
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,48 @@
1
+ # See dry_views/rails2_two for comments... this is the same logic applied to the older ActionView @_content_for chain
2
+
3
+ module ActionView
4
+ module Helpers
5
+ module CaptureHelper
6
+
7
+ def content_for_with_default(name, *args, &block)
8
+ if content_check = content_for?(name)
9
+ content_for_without_empty_check(name) unless content_check == :empty
10
+ elsif block_given?
11
+ content_for_without_empty_check(name, yield)
12
+ else
13
+ options = args.extract_options!
14
+ if options.empty?
15
+ content_for_without_empty_check(name, args.first)
16
+ else
17
+ render options
18
+ end
19
+ end
20
+ end
21
+
22
+ def content_for_with_empty_check(name, content = nil, &block)
23
+ if content_for?(name) == :empty
24
+ return nil
25
+ else
26
+ content_for_without_empty_check(name, content, &block)
27
+ end
28
+ end
29
+ alias_method_chain :content_for, :empty_check
30
+
31
+ def content_for_with_empty_check?(name)
32
+ if @_content_for[name] == DryViews::EMPTY_CONTENT
33
+ return :empty
34
+ elsif content_for_without_empty_check?(name)
35
+ return true
36
+ else
37
+ return false
38
+ end
39
+ end
40
+ alias_method_chain :content_for?, :empty_check
41
+
42
+ def no_content_for(name)
43
+ @_content_for[name] << DryViews::EMPTY_CONTENT
44
+ end
45
+
46
+ end
47
+ end
48
+ end