back_mark 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ nbproject
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in back_mark.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,13 @@
1
+ BackMark
2
+ =========
3
+
4
+ Introduction goes here.
5
+
6
+
7
+ Example
8
+ =======
9
+
10
+ Example goes here.
11
+
12
+
13
+ Copyright (c) 2008 [name of plugin creator], released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/back_mark.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "back_mark/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "back_mark"
7
+ s.version = BackMark::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Satyaram B V"]
10
+ s.email = ["bvsatyaram AT gmail DOT com"]
11
+ s.homepage = "http://bvsatyaram.com"
12
+ s.summary = %q{Mark pages with labels that can be linked back from future pages as back links}
13
+ s.description = %q{Mark pages with labels that can be linked back from future pages as back links}
14
+
15
+ s.rubyforge_project = "back_mark"
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
+ end
@@ -0,0 +1,2 @@
1
+ ActionController::Base.send :include, BackMark::ControllerMethods
2
+ ActionView::Base.send :include, BackMark::ViewMethods
@@ -0,0 +1,3 @@
1
+ module BackMark
2
+ VERSION = "0.0.1"
3
+ end
data/lib/back_mark.rb ADDED
@@ -0,0 +1,132 @@
1
+ # = Back mark gem
2
+ #
3
+ # Mark pages with labels that can be linked back from future pages as back
4
+ # links
5
+ #
6
+ # Also remembers the last accessed non Create/Update/Delete page (as in CRUD).
7
+ # For instance, a user is viewing items index, and navigates to 'new item'
8
+ # page. Once he is done with the creation of the item (i.e., POST to items/create),
9
+ # it would be nice if the user is taken to the index page rather than the 'new'
10
+ # page, which was the LAST VISITED page. Hence, such CUD actions are
11
+ # *_not_remembered_*.
12
+ #
13
+ # Author: Satyaram B V
14
+ #
15
+ # Inspired by back mark plugin authored by Vikram Venkatesan
16
+
17
+ require "back_mark/core_extensions"
18
+
19
+ module BackMark
20
+ module ControllerMethods
21
+ # Pages/Actions which we don't want to remember. We wouldnt ideally want to
22
+ # link back to new, edit, etc.,. We would only want to provide a link to the
23
+ # page that led to those pages
24
+ #
25
+ IGNORE_ACTIONS = %w(new edit create update destroy)
26
+
27
+ def self.included(controller)
28
+ controller.send :include, InstanceMethods
29
+ controller.before_filter :back_mark_pages
30
+ end
31
+
32
+ module InstanceMethods
33
+ # Marks the current url with the given label. Invoke from an action with
34
+ # a meaningful label, if you want that page to be linked back from future
35
+ # pages
36
+ #
37
+ # ==== Params
38
+ # label :: label for the back mark
39
+ # url_to_mark :: the url to remember instead of the current request
40
+ # url
41
+ # mark_now :: Mark the location so that the back link can be rendered
42
+ # in the current action
43
+ #
44
+ # ===== Examples
45
+ # back_mark("Inbox")
46
+ # back_mark("Home")
47
+ # back_mark("Login", '/login', true)
48
+ #
49
+ def back_mark(label, url_to_mark = request.url, mark_now = false)
50
+ # Ignore AJAX requests since they cannot be linked back.
51
+ return if request.xhr?
52
+
53
+ # Mark directly into back_url directly so that we can render in the
54
+ # current action itself.
55
+ if mark_now
56
+ session[:back_label] = session[:prev_label] = label
57
+ session[:back_url] = session[:prev_label] = url_to_mark
58
+ @back_marked = true
59
+ return
60
+ end
61
+
62
+ # Set back url and label from previously back marked page
63
+ session[:back_url] = session[:prev_url]
64
+ session[:back_label] = session[:prev_label]
65
+
66
+ # Mark the current page
67
+ session[:prev_url] = url_to_mark
68
+ session[:prev_label] = label
69
+ @back_marked = true
70
+ end
71
+
72
+ # Redirect to the back link stored in the session or redirect to the
73
+ # default url passed.
74
+ #
75
+ # NOTE: We redirect back to the url stored by the filter. Not the last
76
+ # back_marked url.
77
+ #
78
+ def redirect_to_back_mark_or_default(default_url)
79
+ redirect_to((@marked_in_filter ? session[:filter_back_url] :
80
+ session[:filter_prev_url]) || default_url)
81
+
82
+ session[:filter_back_url] = nil
83
+ end
84
+ end
85
+
86
+ # Add this as a before filter for marking the current request url
87
+ def back_mark_pages(options = {})
88
+ options_to_use = {:force_mark => false}.merge(options)
89
+
90
+ # Ignore AJAX requests since they cannot be linked back.
91
+ # Also ignore actions in IGNORE_ACTIONS
92
+ return if request.xhr? || (!options_to_use[:force_mark] && IGNORE_ACTIONS.include?(params[:action]))
93
+
94
+ session[:filter_back_url] = session[:filter_prev_url]
95
+ session[:filter_prev_url] = options_to_use[:url] || request.url
96
+ @marked_in_filter = true
97
+ end
98
+ end
99
+
100
+ module ViewMethods
101
+ # Returns the stored back url or the given default_url if the former is
102
+ # empty
103
+ #
104
+ def back_url_or_default(default_url)
105
+ (@marked_in_filter ? session[:filter_back_url] :
106
+ session[:filter_prev_url]) || default_url
107
+ end
108
+
109
+ # Returns the last back_marked page url
110
+ def back_url
111
+ @back_marked ? session[:back_url] : session[:prev_url]
112
+ end
113
+
114
+ # Returns the last back_marked label
115
+ def back_label
116
+ @back_marked ? session[:back_label] : session[:prev_label]
117
+ end
118
+
119
+ # Renders a link back to the previous page stored in the session with the
120
+ # stored label if both of them are available
121
+ #
122
+ def render_back_link
123
+ # Dont render back link if the back url is the same as the current page
124
+ return if back_url == request.url
125
+
126
+ if back_url && back_label
127
+ link_to "« Back to '#{back_label}'", back_url, :id => 'back_link'
128
+ end
129
+ end
130
+ end
131
+ end
132
+
@@ -0,0 +1,11 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
+
3
+ require 'test/unit'
4
+ require 'back_mark'
5
+
6
+ class BackMarkTest < Test::Unit::TestCase
7
+ # Replace this with your real tests.
8
+ def test_this_gem
9
+ flunk
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: back_mark
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Satyaram B V
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-03-27 00:00:00 +05:30
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Mark pages with labels that can be linked back from future pages as back links
22
+ email:
23
+ - bvsatyaram AT gmail DOT com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - README
34
+ - Rakefile
35
+ - back_mark.gemspec
36
+ - lib/back_mark.rb
37
+ - lib/back_mark/core_extensions.rb
38
+ - lib/back_mark/version.rb
39
+ - test/back_mark_test.rb
40
+ has_rdoc: true
41
+ homepage: http://bvsatyaram.com
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project: back_mark
66
+ rubygems_version: 1.3.6
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Mark pages with labels that can be linked back from future pages as back links
70
+ test_files: []
71
+