dismissible_helpers 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -35,18 +35,33 @@ the dismissed helpers.
35
35
 
36
36
  To add a dismissible helper to the page, call
37
37
  `render_dismissible_helper`. This helper method will only display the
38
- help text if it has not been dismissed by the user. The string passed to
39
- the method will be processed by the I18n method `t`, so the content of
40
- the help message should be stored in your localization file.
38
+ help text if it has not been dismissed by the user.
39
+
40
+ #### Without a block
41
+
42
+ The string passed to the method will be processed by the I18n method `t`, so the content of the help message should be stored in your localization file.
41
43
 
42
44
  ```erb
43
45
  <h2>Sample Page</h2>
44
46
  <%= render_dismissible_helper 'sample_page.help_text' %>
45
47
  ```
46
48
 
49
+ #### With a block
50
+
51
+ The string passed to the method will be used as a key, to track whether or not the user has dismissed it. The contents of the block will appear inside the dismissible div. This is particularly useful if you want to have a more complex structure in the block.
52
+
53
+ ```erb
54
+ <%= render_dismissible_helper 'how_to_buy' do %>
55
+ <h2>How to buy stuff</h2>
56
+ <p>To buy stuff, click look at this:</p>
57
+ <img src="http://domain.com/img.jpg">
58
+ <% end %>
59
+ ```
60
+
61
+
47
62
  ### Including the routes ###
48
63
 
49
- Add `dismissible_helper_routes` to your `config/routes.rb` file.
64
+ Add `dismissible_helpers_routes` to your `config/routes.rb` file.
50
65
 
51
66
  ### Including the JavaScript ##
52
67
 
@@ -54,7 +69,7 @@ Add the following to your `app/assets/javascripts/application.js`
54
69
 
55
70
  ```javascript
56
71
  // Your other require file statements
57
- //= require dismissed_helpers
72
+ //= require dismissible_helpers
58
73
 
59
74
  $(function(){
60
75
  $('.dismissible').dismissible(); //The view helper applies a
@@ -0,0 +1,4 @@
1
+ en:
2
+ dismissible_helpers:
3
+ actions:
4
+ close: "Close"
@@ -6,6 +6,10 @@ module DismissibleHelpers
6
6
  autoload :Controllers
7
7
  autoload :Routes
8
8
  autoload :Helpers
9
+ autoload :ContentBuilder
10
+ autoload :DismissedChecker
11
+ autoload :DismissedHelpersExtractor
12
+
9
13
  end
10
14
 
11
15
  ActionDispatch::Routing::Mapper.send(:include, DismissibleHelpers::Routes)
@@ -0,0 +1,37 @@
1
+ class DismissibleHelpers::ContentBuilder
2
+ include ActionView::Context
3
+ include ActionView::Helpers::TagHelper
4
+ include ActionView::Helpers::TranslationHelper
5
+
6
+ def self.build(*args)
7
+ self.new(*args).build
8
+ end
9
+
10
+ def initialize(name, contents=nil)
11
+ @name = name
12
+ @contents = contents
13
+ end
14
+
15
+ def build
16
+ options = {}
17
+ options[:class] = 'dismissible'
18
+ options[:data] = {:dismissible_name => @name}
19
+ html = [content_str, close_str].join.html_safe
20
+ content_tag :div, html, options
21
+ end
22
+
23
+ private
24
+
25
+ def content_str
26
+ @content_str ||= if @contents
27
+ @contents
28
+ else
29
+ t(@name)
30
+ end
31
+ end
32
+
33
+ def close_str
34
+ content_tag(:a, I18n.t(:"dismissible_helpers.actions.close"), :class => 'close', :href => '#')
35
+ end
36
+
37
+ end
@@ -0,0 +1,23 @@
1
+ class DismissibleHelpers::DismissedChecker
2
+
3
+ def self.dismissed?(*args)
4
+ self.new(*args).dismissed?
5
+ end
6
+
7
+ def initialize(key, user=nil, dismissed=nil)
8
+ @key = key
9
+ @dismissed_helpers = DismissibleHelpers::DismissedHelpersExtractor.
10
+ extract(user, dismissed)
11
+ end
12
+
13
+ def dismissed?
14
+ dismissed_helpers.include? @key
15
+ end
16
+
17
+ private
18
+
19
+ def dismissed_helpers
20
+ @dismissed_helpers
21
+ end
22
+
23
+ end
@@ -0,0 +1,24 @@
1
+ class DismissibleHelpers::DismissedHelpersExtractor
2
+
3
+ def self.extract(*args)
4
+ self.new(*args).extract
5
+ end
6
+
7
+ def initialize(user, cookie)
8
+ @user = user
9
+ @cookie = cookie
10
+ end
11
+
12
+ def extract
13
+ if @user && @user.respond_to?(:dismissed_helpers)
14
+ @user.dismissed_helpers
15
+ else
16
+ if @cookie.present?
17
+ @cookie.split '|'
18
+ else
19
+ []
20
+ end
21
+ end
22
+ end
23
+
24
+ end
@@ -1,27 +1,21 @@
1
1
  module DismissibleHelpers::Helpers::DismissibleHelpers
2
- def render_dismissible_helper(name)
3
- unless has_been_dismissed?(name)
4
- content_tag :div, :class => 'dismissible', :data => { :dismissible_name => name } do
5
- html = t(name)
6
- html << "\n"
7
- html << content_tag(:a, 'Close', :class => 'close', :href => '#')
8
- html.html_safe
9
- end
2
+
3
+ def render_dismissible_helper(name, &block)
4
+ unless dismissed?(name)
5
+ contents = block_given? ? capture(&block) : nil
6
+ DismissibleHelpers::ContentBuilder.build(name, contents)
10
7
  end
11
8
  end
12
9
 
13
10
  private
14
11
 
15
- def has_been_dismissed?(name)
16
- if respond_to?(:current_user) && current_user && current_user.respond_to?(:dismissed_helpers)
17
- dismissed_helpers = current_user.dismissed_helpers
18
- else
19
- if cookies[:dismissed_helpers].present?
20
- dismissed_helpers = cookies[:dismissed_helpers].split '|'
21
- else
22
- dismissed_helpers = []
23
- end
24
- end
25
- dismissed_helpers.include? name
12
+ def dismissed?(name)
13
+ user = respond_to?(:current_user) ? current_user : nil
14
+ DismissibleHelpers::DismissedChecker.dismissed?(
15
+ name,
16
+ user,
17
+ cookies[:dismissed_helpers]
18
+ )
26
19
  end
20
+
27
21
  end
@@ -1,3 +1,3 @@
1
1
  module DismissibleHelpers
2
- VERSION = '0.1.5'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe DismissibleHelpers::ContentBuilder do
4
+
5
+ describe '#build' do
6
+ context 'given a string' do
7
+ it 'should build a dismissible div containing the string as its contents' do
8
+ content = "<p>Great</p>"
9
+ builder = described_class.new(content)
10
+ html = builder.build
11
+ doc = Capybara.string(html)
12
+ p_el = doc.find('div.dismissible p')
13
+ p_el.text.should == 'Great'
14
+ end
15
+ end
16
+
17
+ it 'should have a close button' do
18
+ builder = described_class.new('something')
19
+ html = builder.build
20
+ doc = Capybara.string(html)
21
+ close_el = doc.find('div a.close')
22
+ close_el[:href].should == '#'
23
+ end
24
+ end
25
+
26
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe DismissibleHelpers::DismissedChecker do
4
+
5
+ describe '#dismissed?' do
6
+ context 'when the dismissed_helpers includes the key' do
7
+ it 'should return true' do
8
+ checker = described_class.new('a')
9
+ checker.stubs(:dismissed_helpers).returns(['a'])
10
+ checker.should be_dismissed
11
+ end
12
+ end
13
+
14
+ context 'the dismissed_helpers does not include the key' do
15
+ it 'should return false' do
16
+ checker = described_class.new('a')
17
+ checker.stubs(:dismissed_helpers).returns([])
18
+ checker.should_not be_dismissed
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe DismissibleHelpers::DismissedHelpersExtractor do
4
+
5
+ describe '#extract' do
6
+ context 'the user exists and has the dismissed_helpers field' do
7
+ it 'should return the user#dismissed_helpers' do
8
+ user = build_stubbed(:user, :dismissed_helpers => ['a'])
9
+ extractor = described_class.new(user, nil)
10
+ extractor.extract.should == ['a']
11
+ end
12
+ end
13
+
14
+ context 'the user does not exist or exists but does not have the dismissed_helpers field' do
15
+ context 'the cookie exists' do
16
+ it 'should return the cookie split into an array' do
17
+ extractor = described_class.new(nil, 'a|c|b')
18
+ extractor.extract.should == %w(a c b)
19
+ end
20
+ end
21
+
22
+ context 'the cookie does not exist' do
23
+ it 'should return an empty array' do
24
+ extractor = described_class.new(nil, nil)
25
+ extractor.extract.should == []
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,3 @@
1
+ <%= render_dismissible_helper 'helper_block' do %>
2
+ <p>Amazing matey!</p>
3
+ <% end %>
@@ -1 +1 @@
1
- <%= render_dismissible_helper 'helper' %>
1
+ <%= render_dismissible_helper 'hello' %>
@@ -2,5 +2,6 @@ Dummy::Application.routes.draw do
2
2
  dismissible_helpers_routes
3
3
  easy_auth_routes
4
4
 
5
+ match 'landing/block', :to => 'landing#block'
5
6
  root :to => 'landing#show'
6
7
  end
@@ -3,13 +3,13 @@ require 'spec_helper'
3
3
  feature 'Signed in user', :js do
4
4
  scenario 'Sees and dismisses helper' do
5
5
  sign_in_as(create(:user))
6
- page.should have_content 'Helper'
6
+ page.should have_content 'Hello world'
7
7
  click_link 'Close'
8
8
  sleep 1
9
- page.should_not have_content 'Helper'
9
+ page.should_not have_content 'Hello world'
10
10
  visit root_path
11
- page.should_not have_content 'Helper'
11
+ page.should_not have_content 'Hello world'
12
12
  visit sign_out_path
13
- page.should have_content 'Helper'
13
+ page.should have_content 'Hello world'
14
14
  end
15
15
  end
@@ -3,11 +3,11 @@ require 'spec_helper'
3
3
  feature 'Signed out user', :js do
4
4
  scenario 'Sees and dismisses helper' do
5
5
  visit root_path
6
- page.should have_content 'Helper'
6
+ page.should have_content 'Hello world'
7
7
  click_link 'Close'
8
8
  sleep 1
9
- page.should_not have_content 'Helper'
9
+ page.should_not have_content 'Hello world'
10
10
  visit root_path
11
- page.should_not have_content 'Helper'
11
+ page.should_not have_content 'Hello world'
12
12
  end
13
13
  end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'landing/block.html.erb' do
4
+
5
+ it 'should display the dismissible helper rendered with a block' do
6
+ render
7
+ doc = Capybara.string(rendered)
8
+ p = doc.find('p')
9
+ p.text.should include('Amazing matey!')
10
+ end
11
+
12
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dismissible_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-10 00:00:00.000000000 Z
12
+ date: 2013-02-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -197,8 +197,12 @@ files:
197
197
  - app/assets/javascripts/dismissible_helpers.coffee
198
198
  - app/controllers/dismissed_helpers_controller.rb
199
199
  - app/helpers/dismissible_helpers_helper.rb
200
+ - config/locales/en.yml
201
+ - lib/dismissible_helpers/content_builder.rb
200
202
  - lib/dismissible_helpers/controllers/dismissed_helpers.rb
201
203
  - lib/dismissible_helpers/controllers.rb
204
+ - lib/dismissible_helpers/dismissed_checker.rb
205
+ - lib/dismissible_helpers/dismissed_helpers_extractor.rb
202
206
  - lib/dismissible_helpers/engine.rb
203
207
  - lib/dismissible_helpers/helpers/dismissible_helpers.rb
204
208
  - lib/dismissible_helpers/helpers.rb
@@ -214,6 +218,9 @@ files:
214
218
  - spec/config/database_cleaner.rb
215
219
  - spec/config/poltergeist.rb
216
220
  - spec/config/rspec.rb
221
+ - spec/content_builder_spec.rb
222
+ - spec/dismissed_checker_spec.rb
223
+ - spec/dismissed_helpers_extractor_spec.rb
217
224
  - spec/dummy/app/assets/javascripts/application.coffee
218
225
  - spec/dummy/app/assets/stylesheets/application.css
219
226
  - spec/dummy/app/controllers/application_controller.rb
@@ -221,6 +228,7 @@ files:
221
228
  - spec/dummy/app/controllers/sessions_controller.rb
222
229
  - spec/dummy/app/helpers/application_helper.rb
223
230
  - spec/dummy/app/models/user.rb
231
+ - spec/dummy/app/views/landing/block.html.erb
224
232
  - spec/dummy/app/views/landing/show.html.erb
225
233
  - spec/dummy/app/views/layouts/application.html.erb
226
234
  - spec/dummy/config/application.rb
@@ -283,6 +291,7 @@ files:
283
291
  - spec/requests/unauthenticated_spec.rb
284
292
  - spec/spec_helper.rb
285
293
  - spec/support/factories.rb
294
+ - spec/views/landing/block_spec.rb
286
295
  homepage: ''
287
296
  licenses: []
288
297
  post_install_message:
@@ -297,7 +306,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
297
306
  version: '0'
298
307
  segments:
299
308
  - 0
300
- hash: 3152705794345596771
309
+ hash: -3362041527304697344
301
310
  required_rubygems_version: !ruby/object:Gem::Requirement
302
311
  none: false
303
312
  requirements:
@@ -306,7 +315,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
306
315
  version: '0'
307
316
  segments:
308
317
  - 0
309
- hash: 3152705794345596771
318
+ hash: -3362041527304697344
310
319
  requirements: []
311
320
  rubyforge_project:
312
321
  rubygems_version: 1.8.23
@@ -320,6 +329,9 @@ test_files:
320
329
  - spec/config/database_cleaner.rb
321
330
  - spec/config/poltergeist.rb
322
331
  - spec/config/rspec.rb
332
+ - spec/content_builder_spec.rb
333
+ - spec/dismissed_checker_spec.rb
334
+ - spec/dismissed_helpers_extractor_spec.rb
323
335
  - spec/dummy/app/assets/javascripts/application.coffee
324
336
  - spec/dummy/app/assets/stylesheets/application.css
325
337
  - spec/dummy/app/controllers/application_controller.rb
@@ -327,6 +339,7 @@ test_files:
327
339
  - spec/dummy/app/controllers/sessions_controller.rb
328
340
  - spec/dummy/app/helpers/application_helper.rb
329
341
  - spec/dummy/app/models/user.rb
342
+ - spec/dummy/app/views/landing/block.html.erb
330
343
  - spec/dummy/app/views/landing/show.html.erb
331
344
  - spec/dummy/app/views/layouts/application.html.erb
332
345
  - spec/dummy/config/application.rb
@@ -389,3 +402,4 @@ test_files:
389
402
  - spec/requests/unauthenticated_spec.rb
390
403
  - spec/spec_helper.rb
391
404
  - spec/support/factories.rb
405
+ - spec/views/landing/block_spec.rb