happy-titles 1.1.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+ describe HappyTitles::Template do
3
+ subject do
4
+ described_class.new(
5
+ name: name,
6
+ with_title: with_title,
7
+ without_title: without_title
8
+ )
9
+ end
10
+
11
+ let(:name) { :my_template }
12
+ let(:with_title) { ":title | :site" }
13
+ let(:without_title) { ":site | :tagline" }
14
+
15
+ it "has a name" do
16
+ expect(subject.name).to eq(:my_template)
17
+ end
18
+
19
+ context "When name is a string" do
20
+ let(:name) { "my_template" }
21
+
22
+ it "changes the name to a symbol" do
23
+ expect(subject.name).to eq(:my_template)
24
+ end
25
+ end
26
+
27
+ describe "Rendering the template" do
28
+ let(:render) { subject.render(**options) }
29
+
30
+ let(:title) { nil }
31
+ let(:options) do
32
+ {
33
+ title: title,
34
+ site: "My site",
35
+ tagline: "My tagline"
36
+ }
37
+ end
38
+
39
+ it "renders without a title" do
40
+ expect(render).to eq("My site | My tagline")
41
+ end
42
+
43
+ context "When the title is set" do
44
+ let(:title) { "My title" }
45
+
46
+ it "renders with a title" do
47
+ expect(render).to eq("My title | My site")
48
+ end
49
+ end
50
+
51
+ context "when the template only has a single format" do
52
+ let(:with_title) { nil }
53
+ let(:without_title) { ":site (:title)" }
54
+
55
+ it "renders with a title" do
56
+ expect(render).to eq("My site (My tagline)")
57
+ end
58
+
59
+ context "When the title is set" do
60
+ let(:title) { "My title" }
61
+
62
+ it "renders with a title" do
63
+ expect(render).to eq("My site (My title)")
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,25 +1,72 @@
1
- # coding: utf-8
2
- require 'rubygems'
1
+ # frozen_string_literal: true
2
+ # This file was generated by the `rspec --init` command. Conventionally, all
3
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
4
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
5
+ # this file to always be loaded, without a need to explicitly require it in any
6
+ # files.
7
+ #
8
+ # Given that it is always loaded, you are encouraged to keep this file as
9
+ # light-weight as possible. Requiring heavyweight dependencies from this file
10
+ # will add to the boot time of your test suite on EVERY test run, even for an
11
+ # individual file that may not need all of that loaded. Instead, consider making
12
+ # a separate helper file that requires the additional dependencies and performs
13
+ # the additional setup, and require it from the spec files that actually need
14
+ # it.
15
+ #
16
+ # The `.rspec` file also contains a few flags that are not defaults but that
17
+ # users commonly want.
18
+ #
19
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
3
20
 
4
- gem 'activesupport', '~> 3.0'
5
- gem 'actionpack', '~> 3.0'
6
- require 'active_support'
7
- require 'action_pack'
8
- require 'action_view'
9
- require 'action_controller'
21
+ require "rubygems"
22
+ require "bundler/setup"
10
23
 
11
- gem 'rspec', '~> 2.5'
12
- gem 'rspec-rails', '~> 2.5'
13
- gem 'hpricot', '>= 0.6.1'
14
- gem 'rspec_tag_matchers', '>= 1.0.0'
15
- require 'rspec_tag_matchers'
24
+ require "rails/all"
25
+ require "rspec/rails"
26
+ require "rspec-html-matchers"
27
+ require "fake_app"
16
28
 
17
- require File.join(File.dirname(__FILE__), "../lib/happy-titles")
29
+ require "happy-titles"
30
+
31
+ FakeApp::Application.initialize!
18
32
 
19
33
  RSpec.configure do |config|
20
- config.include(RspecTagMatchers)
21
- end
34
+ config.include(RSpecHtmlMatchers)
35
+
36
+ # rspec-expectations config goes here. You can use an alternate
37
+ # assertion/expectation library such as wrong or the stdlib/minitest
38
+ # assertions if you prefer.
39
+ config.expect_with :rspec do |expectations|
40
+ # This option will default to `true` in RSpec 4. It makes the `description`
41
+ # and `failure_message` of custom matchers include text for helper methods
42
+ # defined using `chain`, e.g.:
43
+ # be_bigger_than(2).and_smaller_than(4).description
44
+ # # => "be bigger than 2 and smaller than 4"
45
+ # ...rather than:
46
+ # # => "be bigger than 2"
47
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
48
+ end
49
+
50
+ # rspec-mocks config goes here. You can use an alternate test double
51
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
52
+ config.mock_with :rspec do |mocks|
53
+ # Prevents you from mocking or stubbing a method that does not exist on
54
+ # a real object. This is generally recommended, and will default to
55
+ # `true` in RSpec 4.
56
+ mocks.verify_partial_doubles = true
57
+ end
22
58
 
23
- ActionView::Base.class_eval do
24
- include HappyTitles
25
- end
59
+ # Run specs in random order to surface order dependencies. If you find an
60
+ # order dependency and want to debug it, you can fix the order by providing
61
+ # the seed, which is printed after each run.
62
+ # --seed 1234
63
+ config.order = :random
64
+
65
+ config.after(:each) do
66
+ reset_config
67
+ end
68
+
69
+ def reset_config
70
+ HappyTitles.remove_instance_variable(:@config) if HappyTitles.instance_variable_defined?(:@config)
71
+ end
72
+ end
metadata CHANGED
@@ -1,79 +1,148 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: happy-titles
3
- version: !ruby/object:Gem::Version
4
- hash: 19
5
- prerelease: false
6
- segments:
7
- - 1
8
- - 1
9
- - 0
10
- version: 1.1.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Andy Pearson
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2011-05-07 00:00:00 +01:00
19
- default_executable:
20
- dependencies: []
21
-
22
- description:
23
- email: andy@andy-pearson.com
11
+ date: 2016-03-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-html-matchers
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.38.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.38.0
83
+ description: A simple way to handle page titles in your layouts.
84
+ email:
85
+ - andy@andy-pearson.com
24
86
  executables: []
25
-
26
87
  extensions: []
27
-
28
- extra_rdoc_files:
29
- - README.markdown
30
- files:
31
- - .git_ignore
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".rubocop.yml"
93
+ - ".ruby-version"
94
+ - Gemfile
95
+ - Gemfile.lock
32
96
  - MIT-LICENCE
33
- - README.markdown
97
+ - README.md
98
+ - RELEASE.md
34
99
  - Rakefile
35
- - VERSION
100
+ - TODO.md
36
101
  - happy-titles.gemspec
37
- - init.rb
38
102
  - lib/happy-titles.rb
103
+ - lib/happy-titles/config.rb
104
+ - lib/happy-titles/helpers.rb
39
105
  - lib/happy-titles/railtie.rb
40
- - rails/init.rb
41
- - spec/happy_titles_spec.rb
106
+ - lib/happy-titles/template.rb
107
+ - lib/happy-titles/template_set.rb
108
+ - lib/happy-titles/version.rb
109
+ - spec/fake_app.rb
110
+ - spec/lib/config_spec.rb
111
+ - spec/lib/happy_titles_spec.rb
112
+ - spec/lib/helpers_spec.rb
113
+ - spec/lib/template_set_spec.rb
114
+ - spec/lib/template_spec.rb
42
115
  - spec/spec_helper.rb
43
- has_rdoc: true
44
- homepage: http://github.com/andypearson/happy-titles
45
- licenses: []
46
-
116
+ homepage: https://github.com/andypearson/happy-titles
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
47
120
  post_install_message:
48
121
  rdoc_options: []
49
-
50
- require_paths:
122
+ require_paths:
51
123
  - lib
52
- required_ruby_version: !ruby/object:Gem::Requirement
53
- none: false
54
- requirements:
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
55
126
  - - ">="
56
- - !ruby/object:Gem::Version
57
- hash: 3
58
- segments:
59
- - 0
60
- version: "0"
61
- required_rubygems_version: !ruby/object:Gem::Requirement
62
- none: false
63
- requirements:
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
64
131
  - - ">="
65
- - !ruby/object:Gem::Version
66
- hash: 3
67
- segments:
68
- - 0
69
- version: "0"
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
70
134
  requirements: []
71
-
72
135
  rubyforge_project:
73
- rubygems_version: 1.3.7
136
+ rubygems_version: 2.5.1
74
137
  signing_key:
75
- specification_version: 3
138
+ specification_version: 4
76
139
  summary: A simple way to handle page titles in your layouts.
77
- test_files:
78
- - spec/happy_titles_spec.rb
140
+ test_files:
141
+ - spec/fake_app.rb
142
+ - spec/lib/config_spec.rb
143
+ - spec/lib/happy_titles_spec.rb
144
+ - spec/lib/helpers_spec.rb
145
+ - spec/lib/template_set_spec.rb
146
+ - spec/lib/template_spec.rb
79
147
  - spec/spec_helper.rb
148
+ has_rdoc:
data/.git_ignore DELETED
@@ -1 +0,0 @@
1
- .DS_Store
data/README.markdown DELETED
@@ -1,153 +0,0 @@
1
- # Happy Titles!
2
-
3
- A simple (and cheerful) way to handle page titles in your layouts.
4
-
5
- ## Getting Started
6
-
7
- ### Install the Gem
8
-
9
- #### In Rails 3
10
-
11
- Add the following to your Gemfile
12
-
13
- gem 'happy-titles'
14
-
15
- #### In Rails 2
16
-
17
- Run the following:
18
-
19
- sudo gem install happy-titles
20
-
21
- And add it to your environment.rb configuration as a gem dependency:
22
-
23
- config.gem "happy-titles", :lib => 'happy_titles'
24
-
25
- ### Set the Defaults
26
-
27
- Create a new file in `config/initializers` called `happy_titles.rb` or something else which makes sense!
28
-
29
- In this new file, add the following lines to set the default Site and Tagline.
30
-
31
- ActionView::Base.happy_title_setting(:site, 'Your Site')
32
- ActionView::Base.happy_title_setting(:tagline, 'Your witty but informative tagline')
33
-
34
- ### Update your Layout
35
-
36
- In the layout where you want to display your title add the following call to the Happy Titles helper method.
37
-
38
- <%= happy_title %>
39
-
40
- The header element of your layout might end up looking something like:
41
-
42
- <head>
43
- <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
44
- <%= happy_title %>
45
- </head>
46
-
47
- Notice you don't need to wrap the helper in a `<title>` element, this is done for you.
48
-
49
- ### Set the Title in your Views
50
-
51
- In each of your views call the `title` method to set the title for that page.
52
-
53
- <% title 'Your very first Happy Title!' %>
54
-
55
- ### See your Titles!
56
-
57
- Given the above settings, let's have a look at the output you will receive when you call the `happy_title` method in your layouts.
58
-
59
- On pages where the title has not been set you will see...
60
-
61
- <title>Your Site | Your witty but informative tagline</title>
62
-
63
- ...and on pages where the title _has_ been set you will see...
64
-
65
- <title>Your very first Happy Title! | Your Site</title>
66
-
67
- That really is all there is to setting up and using Happy Titles!
68
-
69
-
70
- ## Customising the Title Templates
71
-
72
- ### Intro
73
-
74
- You have already seen how to set up and use Happy Titles, now let's take a look at how to customise the individual title templates.
75
-
76
- In all of the following examples I am going to assume the same defaults that are set up in the Set the Defaults section of this Readme.
77
-
78
- Before we have a look at that, let's have a look at the **placeholders** you can use. These placeholders will be replaced with the real content when the title is rendered.
79
-
80
- :site -> 'Your Site'
81
- :tagline -> 'Your witty but informative tagline'
82
- :title -> Will become 'Your very first Happy Title!' or :tagline if a page title has not been set
83
-
84
- Now let's have a look at the default templates.
85
-
86
- ':site | :title' -> Is used when there is no title set: 'Your Site | Your witty but informative tagline'
87
- ':title | :site' -> Is used when there IS a title set: 'Your very first Happy Title! | Your Site'
88
-
89
- You can change the default templates by using the following setting in `config/initializers/happy_titles.rb`
90
-
91
- # The first param sets the name of the template and takes a symbol
92
- # The second param is the template to use when there is no title set
93
- # The third param is the template to use when there is a title set
94
- ActionView::Base.happy_title_template(:default, '[:site]', '[:site] :title')
95
-
96
- Then when you render your titles you will see the following
97
-
98
- <title>[Your Site]</title> <!-- when there is no title -->
99
- <title>[Your Site] Your very first Happy Title!</title> <!-- when the title is set -->
100
-
101
- ### Adding Additional Templates
102
-
103
- As well as changing the default template, you can also add new templates and then use those when you need a different title format in a different layout.
104
-
105
- # config/initializers/happy_titles.rb
106
- ActionView::Base.happy_title_template(:extra, '++ :site ++', '++ :site ++ :title ++')
107
-
108
- Then in your layout, you can use the following to call the extra template.
109
-
110
- <%= happy_title :extra %>
111
-
112
- And you will get the following output, as you probably would of guessed by now!
113
-
114
- <title>++ Your Site ++</title> <!-- when there is no title -->
115
- <title>++ Your Site ++ Your very first Happy Title! ++</title> <!-- when the title is set -->
116
-
117
- You can add as many additional title templates as you need!
118
-
119
- One final thing to mention, you can create templates that just have one format. So...
120
-
121
- # config/initializers/happy_titles.rb
122
- ActionView::Base.happy_title_template(:single, ':site (:title)')
123
-
124
- # In your layout
125
- <%= happy_title :single %>
126
-
127
- # Output
128
- <title>Your Site (Your witty but informative tagline)</title> <!-- when there is no title -->
129
- <title>Your Site (Your very first Happy Title!)</title> <!-- when the title is set -->
130
-
131
-
132
- ## Other Things...
133
-
134
- ### Why Use Happy Titles?
135
-
136
- Happy Titles is a simple way to use multiple title formats in the same app. In my case, the app I was working on at the time required different title formats for public and private pages, and this is the best way I could think of managing that requirement in a suitably DRY fashion.
137
-
138
- ### Why on earth is it called Happy Titles?
139
-
140
- What can I say, I was in a good mood when I started writing the plugin and I couldn't think of anything better to call it!
141
-
142
- ### Inspiration and Thanks
143
-
144
- When it came to actually writing the plugin I studied the [Headliner plugin][hl] to see how that was done and got some ideas on how to set up my specs. I also want to thank [Justin French][jf] for his help when I created some small patches for his [Formtastic][ft] plugin. He made me realise that even though I don't have a lot of experience I can still help out the open source community. Please take a look at [Formtastic][ft] for a great way to make semantic forms really easy in Rails. Thanks also to all the clever chaps working on GitHub, I don't think I would of ever had the oomph to release this if GitHub wasn't here to make it so wonderfully easy and fun!
145
-
146
- ### Please give me some feedback
147
-
148
- This plugin was written as an experiment and a learning experience. I am a relative newcomer to the world of Ruby, Rails and Github so if you have 5 Minutes to skim read the code and maybe suggest something I could do better in the future then please leave a comment and I will be very grateful of the feedback, thanks in advance!
149
-
150
-
151
- [hl]: http://github.com/mokolabs/headliner/tree/master "mokolabs Headliner on GitHub"
152
- [jf]: http://justinfrench.com/ "Justin French"
153
- [ft]: http://github.com/justinfrench/formtastic/tree/master "justinfrench Formtastic on GitHub"