pk-merb_history 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ == merb_history
2
+
3
+ A plugin for *the* Merb framework that provides ...
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'merb-core'
6
6
  require 'merb-core/tasks/merb'
7
7
 
8
8
  GEM_NAME = "merb_history"
9
- GEM_VERSION = "0.0.1"
9
+ GEM_VERSION = "0.0.2"
10
10
  AUTHOR = "Pavel Kunc"
11
11
  EMAIL = "pavel.kunc@gmail.com"
12
12
  HOMEPAGE = "http://github.com/pk/merb_history"
@@ -18,15 +18,15 @@ spec = Gem::Specification.new do |s|
18
18
  s.version = GEM_VERSION
19
19
  s.platform = Gem::Platform::RUBY
20
20
  s.has_rdoc = true
21
- s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
21
+ s.extra_rdoc_files = ["README.rdoc", "LICENSE", 'TODO']
22
22
  s.summary = SUMMARY
23
23
  s.description = s.summary
24
24
  s.author = AUTHOR
25
25
  s.email = EMAIL
26
26
  s.homepage = HOMEPAGE
27
- s.add_dependency('merb', '>= 1.0.11')
27
+ s.add_dependency('merb-core', '>= 1.0.11')
28
28
  s.require_path = 'lib'
29
- s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
29
+ s.files = %w(LICENSE README.rdoc Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
30
30
 
31
31
  end
32
32
 
data/lib/merb_history.rb CHANGED
@@ -7,6 +7,7 @@ if defined?(Merb::Plugins)
7
7
  require File.join(File.dirname(__FILE__) / "merb_history" / "history" / "history")
8
8
  require File.join(File.dirname(__FILE__) / "merb_history" / "merb" / "controller")
9
9
  require File.join(File.dirname(__FILE__) / "merb_history" / "merb" / "request")
10
+ require File.join(File.dirname(__FILE__) / "merb_history" / "merb" / "url_helpers")
10
11
 
11
12
  Merb::BootLoader.before_app_loads do
12
13
  # require code that must be loaded before the application
@@ -1,3 +1,5 @@
1
+ require 'forwardable'
2
+
1
3
  module MerbHistory
2
4
 
3
5
  class History
@@ -9,7 +11,7 @@ module MerbHistory
9
11
  SESSION_RETURN_TO_KEY = :return_to
10
12
 
11
13
  # Accessors
12
- attr_accessor :max_size, :exclude
14
+ attr_accessor :max_size, :exclude, :return_to
13
15
  attr_reader :history
14
16
 
15
17
  def initialize(session, options = {})
@@ -40,15 +42,6 @@ module MerbHistory
40
42
  !@return_to.empty?
41
43
  end
42
44
 
43
- def return_to(default, keep = false)
44
- if should_return?
45
- url = keep ? @return_to.first : @return_to.pop
46
- else
47
- url = default.is_a? Symbol ? url(default) : default
48
- end
49
- url
50
- end
51
-
52
45
  def init_session(session)
53
46
  session[SESSION_HISTORY_KEY] = [] if session[SESSION_HISTORY_KEY].nil?
54
47
  session[SESSION_RETURN_TO_KEY] = [] if session[SESSION_RETURN_TO_KEY].nil?
@@ -1,24 +1,12 @@
1
1
  module MerbHistory
2
2
  module Controller
3
3
 
4
- def self.included(base)
5
- base.class_eval <<-RUBY
6
- alias original_url url
7
- def url(name, *args)
8
- if args.include?(:return_here)
9
- args.delete(:return_here)
10
- args << {:return_here => 1}
11
- end
12
- original_url(name, *args)
13
- end
14
- RUBY
15
- end
16
-
17
4
  def record_history
18
5
  request.history.push(request)
19
6
  end
20
7
 
21
8
  def init_history(options = {})
9
+ self.class.send(:include, MerbHistory::UrlHelpers)
22
10
  request.history ||= History.new(session, options)
23
11
  end
24
12
 
@@ -9,5 +9,6 @@ module MerbHistory
9
9
  raise ArgumentError, 'Pass MerbHistory::History objects only.' unless h.is_a? History
10
10
  @_history = h
11
11
  end
12
- end
13
- end
12
+
13
+ end #Request
14
+ end #MerbHistory
@@ -1,9 +1,28 @@
1
1
  module MerbHistory
2
2
  module UrlHelpers
3
+
4
+ def self.included(base)
5
+ base.class_eval do
6
+ override! :url
7
+ alias :merb_controller_url :url unless method_defined?(:merb_controller_url)
8
+ alias :url :url_with_normalized_history_params
9
+ end
10
+ end
11
+
12
+ def url_with_normalized_history_params(name, *args)
13
+ return merb_controller_url(name) if args.empty?
14
+ args[args.length - 1] = args.last.merge({:return_here => 1}) if args.last.is_a?(Hash) && args.last.delete(:return_here)
15
+ merb_controller_url(name, *args)
16
+ end
3
17
 
4
- alias :original_url, :url
5
- def url(name, *args)
18
+ def return_to(default, keep = false)
19
+ if request.history.should_return?
20
+ keep ? request.history.return_to.first : request.history.return_to.pop
21
+ else
22
+ default.is_a?(Symbol) ? url(default) : default
23
+ end
6
24
  end
25
+ alias :return_in_history_or_to :return_to
7
26
 
8
- end
9
- end
27
+ end #UrlHelpers
28
+ end #MerbHistory
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  describe MerbHistory::History do
4
4
 
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  describe MerbHistory::Controller do
4
4
 
@@ -6,6 +6,7 @@ describe MerbHistory::Controller do
6
6
 
7
7
  before(:all) do
8
8
  @controller = HistoryfiedController.new(fake_request)
9
+ @controller.init_history
9
10
  end
10
11
 
11
12
  it "should allow to record history" do
@@ -15,6 +16,14 @@ describe MerbHistory::Controller do
15
16
  it "should allow to init history" do
16
17
  @controller.should respond_to(:init_history)
17
18
  end
19
+
20
+ it "should include MerbHistoy URL helpers" do
21
+ @controller.class.ancestors.should include(MerbHistory::UrlHelpers)
22
+ end
23
+
24
+ it "should respond to return_to" do
25
+ @controller.should respond_to(:return_to)
26
+ end
18
27
  end
19
28
 
20
29
  describe "#init_history" do
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe MerbHistory::Request do
4
+ before(:each) do
5
+ @request = fake_request
6
+ end
7
+
8
+ it "should add history method to request" do
9
+ @request.should respond_to(:history)
10
+ end
11
+
12
+ it "should add history= method to request" do
13
+ @request.should respond_to(:history=)
14
+ end
15
+ end
@@ -0,0 +1,83 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe MerbHistory::UrlHelpers do
4
+
5
+ before(:each) do
6
+ @controller = HistoryfiedController.new(fake_request)
7
+ @controller.init_history
8
+ end
9
+
10
+ describe "#url()" do
11
+ it "should normalize return_here parameter and keep other parameters" do
12
+ addr = @controller.url(:home, :return_here => 'yes', :test => 'x')
13
+ addr.should match(/^\/index\?(&|return_here=1|test=x)/)
14
+ end
15
+
16
+ it "should not affect route parameters with return_here and other params" do
17
+ addr = @controller.url(:index_with_params, 1, 2, :return_here => 'yes', :test => 'x')
18
+ addr.should match(/^\/index\/1\/2\?(&|return_here=1|test=x)/)
19
+ end
20
+
21
+ it "should not affect route parameters with return_here" do
22
+ addr = @controller.url(:index_with_params, 1, 2, :return_here => 'yes')
23
+ addr.should match(/^\/index\/1\/2\?return_here=1/)
24
+ end
25
+
26
+ it "should not affect route parameters without return_here" do
27
+ addr = @controller.url(:index_with_params, 1, 2)
28
+ addr.should match(/^\/index\/1\/2/)
29
+ end
30
+ end
31
+
32
+ describe "return_to()" do
33
+ before(:all) do
34
+ # Add history recording and initialization to controller
35
+ class HistoryfiedController
36
+ before :init_history
37
+ after :record_history
38
+ end
39
+ end
40
+
41
+ it "should have verbose alias return_in_history_or_to" do
42
+ HistoryfiedController.new(fake_request).should respond_to(:return_in_history_or_to)
43
+ end
44
+
45
+ describe "without set to return here" do
46
+ before(:each) do
47
+ with_cookies(HistoryfiedController) do
48
+ get('/index')
49
+ get('/show')
50
+ @controller = get('/edit')
51
+ end
52
+ end
53
+
54
+ it "should return default URL" do
55
+ @controller.return_to(:home).should match(/^\/index/)
56
+ end
57
+ end
58
+
59
+ describe "with set to return here" do
60
+ before(:each) do
61
+ with_cookies(HistoryfiedController) do
62
+ get('/index')
63
+ get('/show')
64
+ @controller = get('/edit', {:return_here => 1})
65
+ end
66
+ end
67
+ it "should return URL from history" do
68
+ @controller.return_to(:home).should match(/^\/show/)
69
+ end
70
+
71
+ it "should pop off URL from history when returned" do
72
+ @controller.return_to(:home).should match(/^\/show/)
73
+ @controller.return_to(:home).should match(/^\/index/)
74
+ end
75
+
76
+ it "should not pop off URL from history when returned if we ask to not pop off" do
77
+ @controller.return_to(:home, true).should match(/^\/show/)
78
+ @controller.return_to(:home, true).should match(/^\/show/)
79
+ end
80
+ end
81
+ end
82
+
83
+ end
@@ -3,11 +3,11 @@ require File.dirname(__FILE__) + '/spec_helper'
3
3
  describe "merb_history" do
4
4
 
5
5
  it "should historyfi controllers" do
6
- HistoryfiedController.ancestors.include? MerbHistory::Controller
6
+ HistoryfiedController.ancestors.should include(MerbHistory::Controller)
7
7
  end
8
8
 
9
9
  it "should historyfi request" do
10
- Merb::Request.ancestors.include? MerbHistory::Request
10
+ Merb::Request.ancestors.should include(MerbHistory::Request)
11
11
  end
12
12
 
13
13
  end
data/spec/spec_helper.rb CHANGED
@@ -21,6 +21,8 @@ Spec::Runner.configure do |config|
21
21
  end
22
22
 
23
23
  Merb::Router.prepare do
24
+ match('/index').to(:controller => 'historyfied_controller').name(:home)
25
+ match('/index/:first/:second').to(:controller => 'historyfied_controller').name(:index_with_params)
24
26
  match('/:action').to(:controller => 'historyfied_controller')
25
27
  end
26
28
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pk-merb_history
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Kunc
@@ -9,11 +9,11 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-16 00:00:00 -07:00
12
+ date: 2009-04-22 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: merb
16
+ name: merb-core
17
17
  type: :runtime
18
18
  version_requirement:
19
19
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,12 +29,12 @@ executables: []
29
29
  extensions: []
30
30
 
31
31
  extra_rdoc_files:
32
- - README
32
+ - README.rdoc
33
33
  - LICENSE
34
34
  - TODO
35
35
  files:
36
36
  - LICENSE
37
- - README
37
+ - README.rdoc
38
38
  - Rakefile
39
39
  - TODO
40
40
  - lib/merb_history
@@ -46,11 +46,15 @@ files:
46
46
  - lib/merb_history/merb/url_helpers.rb
47
47
  - lib/merb_history/merbtasks.rb
48
48
  - lib/merb_history.rb
49
+ - spec/history
50
+ - spec/history/history_spec.rb
49
51
  - spec/historyfied_controller.rb
50
52
  - spec/log
51
53
  - spec/log/merb_test.log
52
- - spec/merb_history_controller_spec.rb
53
- - spec/merb_history_history_spec.rb
54
+ - spec/merb
55
+ - spec/merb/controller_spec.rb
56
+ - spec/merb/request_spec.rb
57
+ - spec/merb/url_helpers_spec.rb
54
58
  - spec/merb_history_spec.rb
55
59
  - spec/spec_helper.rb
56
60
  has_rdoc: true
@@ -77,7 +81,7 @@ requirements: []
77
81
  rubyforge_project: merb_history
78
82
  rubygems_version: 1.2.0
79
83
  signing_key:
80
- specification_version: 2
84
+ specification_version: 3
81
85
  summary: Merb plugin that provides history management for user actions (URLs).
82
86
  test_files: []
83
87
 
data/README DELETED
@@ -1,4 +0,0 @@
1
- merb_history
2
- ============
3
-
4
- A plugin for the Merb framework that provides ...