preserve 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +148 -0
  4. data/Rakefile +33 -0
  5. data/lib/preserve.rb +36 -0
  6. data/lib/preserve/version.rb +3 -0
  7. data/spec/dummy/README.rdoc +28 -0
  8. data/spec/dummy/Rakefile +6 -0
  9. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  10. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  11. data/spec/dummy/app/controllers/application_controller.rb +4 -0
  12. data/spec/dummy/app/controllers/parameters_controller.rb +18 -0
  13. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  14. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  15. data/spec/dummy/bin/bundle +3 -0
  16. data/spec/dummy/bin/rails +4 -0
  17. data/spec/dummy/bin/rake +4 -0
  18. data/spec/dummy/bin/setup +29 -0
  19. data/spec/dummy/config.ru +4 -0
  20. data/spec/dummy/config/application.rb +32 -0
  21. data/spec/dummy/config/boot.rb +5 -0
  22. data/spec/dummy/config/database.yml +25 -0
  23. data/spec/dummy/config/environment.rb +5 -0
  24. data/spec/dummy/config/environments/development.rb +41 -0
  25. data/spec/dummy/config/environments/production.rb +79 -0
  26. data/spec/dummy/config/environments/test.rb +42 -0
  27. data/spec/dummy/config/initializers/assets.rb +11 -0
  28. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  29. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  30. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  31. data/spec/dummy/config/initializers/inflections.rb +16 -0
  32. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  33. data/spec/dummy/config/initializers/session_store.rb +3 -0
  34. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  35. data/spec/dummy/config/locales/en.yml +23 -0
  36. data/spec/dummy/config/routes.rb +3 -0
  37. data/spec/dummy/config/secrets.yml +22 -0
  38. data/spec/dummy/db/test.sqlite3 +0 -0
  39. data/spec/dummy/log/test.log +2740 -0
  40. data/spec/dummy/public/404.html +67 -0
  41. data/spec/dummy/public/422.html +67 -0
  42. data/spec/dummy/public/500.html +66 -0
  43. data/spec/dummy/public/favicon.ico +0 -0
  44. data/spec/preserve_spec.rb +46 -0
  45. data/spec/spec_helper.rb +15 -0
  46. data/spec/support/request_helpers.rb +5 -0
  47. metadata +171 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ac09bc7bb243e6a19585afb9a6c9f91d84bebc18
4
+ data.tar.gz: 030f19ee58810f1870009bd2221daccfd5b03a25
5
+ SHA512:
6
+ metadata.gz: f5a7403a8855b2f15e53010e995a018457add9da87c3643ae081afff7f7360f5b5c8a50d46ac5a8c61dd5bb97a44b2fe910c718a1225a39a545296ff07341b51
7
+ data.tar.gz: bcd43a0e6e5ed9486bf8e473d21a8ea56b338a2f0550f0943a9e59c91a5dc1647529c02361b2215874077daf7d356f45cf635a26f6f5cd0b9468ded345ddb751
@@ -0,0 +1,20 @@
1
+ Copyright 2013 Bartosz Pieńkowski
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,148 @@
1
+ # Preserve
2
+
3
+ Preserve is a Rails plugin which stores selected parameters in a session to make them available in subsequent requests.
4
+
5
+ ## Installation
6
+
7
+ Add the following line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'preserve'
11
+ ```
12
+
13
+ Install the gem with Bundler:
14
+
15
+ ```
16
+ $ bundle install
17
+ ```
18
+
19
+ Or do it manually by running:
20
+
21
+ ```
22
+ $ gem install preserve
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ For the sole purpose of this example, assume we have a Rails application with a controller showing all parameters sent in a request.
28
+
29
+ ```ruby
30
+ class ParametersController < ApplicationController
31
+ def index
32
+ render json: request_parameters
33
+ end
34
+
35
+ private
36
+
37
+ def request_parameters
38
+ params.except(:controller, :action)
39
+ end
40
+ end
41
+ ```
42
+
43
+ Routes are declared as following:
44
+
45
+ ```ruby
46
+ Rails.application.routes.draw do
47
+ resources :parameters, only: :index
48
+ end
49
+ ```
50
+
51
+ Let's start the application and test its behaviour using [cURL](http://curl.haxx.se/).
52
+ The whole concept is based on a session, so in order for this to work, the cookie engine must be enabled (hence `-c` and `-b` options).
53
+
54
+ In the first request, the `per_page` parameter is set to 20.
55
+
56
+ ```
57
+ $ curl -c cookies http://localhost:3000/parameters?per_page=20
58
+ ```
59
+ ```json
60
+ {"per_page":"20"}
61
+ ```
62
+
63
+ As expected, the application returns the parameter and its value.
64
+
65
+ The next request is sent without any parameters.
66
+
67
+ ```
68
+ $ curl -b cookies http://localhost:3000/parameters
69
+ ```
70
+ ```json
71
+ {}
72
+ ```
73
+
74
+ Obviously, the `per_page` parameter is no longer available.
75
+
76
+ Now, let's call the `preserve` method inside the `ParametersController` class with the parameter name as an argument.
77
+
78
+ ```ruby
79
+ class ParametersController < ApplicationController
80
+ preserve :per_page
81
+
82
+ # ...
83
+ end
84
+ ```
85
+
86
+ Sending the same two requests again gives a different result.
87
+
88
+ ```
89
+ $ curl -c cookies http://localhost:3000/parameters?per_page=20
90
+ ```
91
+ ```json
92
+ {"per_page":"20"}
93
+ ```
94
+
95
+ ```
96
+ $ curl -b cookies http://localhost:3000/parameters
97
+ ```
98
+ ```json
99
+ {"per_page":"20"}
100
+ ```
101
+
102
+ This time, the `per_page` parameter is still available when the second request is made, even though it wasn't sent particularly in that request.
103
+
104
+ ### Multiple arguments
105
+
106
+ If more than one parameter needs to be preserved within the same controller, its name can be passed as a succeeding argument to the `preserve` method.
107
+
108
+ ```ruby
109
+ preserve :per_page, :page
110
+ ```
111
+
112
+ ### Restrictions
113
+
114
+ Limiting functionality provided by the gem to a certain set of controller actions can be achieved by applying the `:only` (or `:except`) option.
115
+
116
+ ```ruby
117
+ preserve :per_page, only: :index
118
+ ```
119
+
120
+ It behaves exactly like the `:only` option of an [Action Controller filter](http://guides.rubyonrails.org/action_controller_overview.html#filters).
121
+ In fact, the gem sets such filter underneath, so you can make use of all its options – they will be passed to that filter.
122
+
123
+ ### Application-wide parameters
124
+
125
+ When there's a need to store a parameter used across the whole application, the `preserve` method should be called inside the `ApplicationController`.
126
+
127
+ ```ruby
128
+ class ApplicationController < ActionController::Base
129
+ preserve :locale
130
+ end
131
+ ```
132
+
133
+ In more complex scenarios, controller inheritance can be utilized to further adjust the scope.
134
+
135
+ ### Setting a session key prefix
136
+
137
+ By default, parameter values are stored in a session with a key that consists of a controller name and parameter name (e.g. `users_order` for the `order` parameter in the `UsersController`).
138
+
139
+ In most cases such combination results in a unique session key, but there might be a situation when it's necessary to add a prefix in order to avoid conflicts with a session key that is already in use.
140
+ It can be done by passing the `:prefix` option.
141
+
142
+ ```ruby
143
+ class UsersController < ApplicationController
144
+ preserve :order, prefix: 'preserved'
145
+ end
146
+ ```
147
+
148
+ From now on, the parameter will be stored in a session with the `preserved_users_order` key.
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env rake
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ begin
10
+ require 'rdoc/task'
11
+ rescue LoadError
12
+ require 'rdoc/rdoc'
13
+ require 'rake/rdoctask'
14
+ RDoc::Task = Rake::RDocTask
15
+ end
16
+
17
+ RDoc::Task.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'Preserve'
20
+ rdoc.options << '--line-numbers'
21
+ rdoc.rdoc_files.include('README.rdoc')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
27
+ require 'rspec/core'
28
+ require 'rspec/core/rake_task'
29
+
30
+ desc 'Run all specs in spec directory (excluding plugin specs)'
31
+ RSpec::Core::RakeTask.new(:spec)
32
+
33
+ task default: :spec
@@ -0,0 +1,36 @@
1
+ require 'active_support'
2
+
3
+ module Preserve
4
+ def self.filter(name, key)
5
+ lambda do
6
+ if params[name].blank?
7
+ value = session[key.to_sym]
8
+ params[name] = value if value.present?
9
+ else
10
+ session[key.to_sym] = params[name]
11
+ end
12
+ end
13
+ end
14
+
15
+ def preserve(*parameters)
16
+ options = parameters.extract_options!
17
+ prefix = options.delete(:prefix)
18
+
19
+ parameters.each do |name|
20
+ key = [prefix, controller_name, name].compact.join('_')
21
+ _preserve_filter(options, &Preserve.filter(name, key))
22
+ end
23
+ end
24
+
25
+ def _preserve_filter(*args, &block)
26
+ if respond_to?(:before_action)
27
+ before_action(*args, &block)
28
+ else
29
+ before_filter(*args, &block)
30
+ end
31
+ end
32
+ end
33
+
34
+ ActiveSupport.on_load(:action_controller) do
35
+ extend Preserve
36
+ end
@@ -0,0 +1,3 @@
1
+ module Preserve
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery with: :exception
3
+ preserve :locale
4
+ end
@@ -0,0 +1,18 @@
1
+ class ParametersController < ApplicationController
2
+ preserve :per_page, :page, only: :index
3
+ preserve :order, prefix: 'preserved'
4
+
5
+ def index
6
+ render json: request_parameters
7
+ end
8
+
9
+ def create
10
+ render json: request_parameters
11
+ end
12
+
13
+ private
14
+
15
+ def request_parameters
16
+ params.except(:controller, :action)
17
+ end
18
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
+ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pathname'
3
+
4
+ # path to your application root.
5
+ APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
6
+
7
+ Dir.chdir APP_ROOT do
8
+ # This script is a starting point to setup your application.
9
+ # Add necessary setup steps to this file:
10
+
11
+ puts "== Installing dependencies =="
12
+ system "gem install bundler --conservative"
13
+ system "bundle check || bundle install"
14
+
15
+ # puts "\n== Copying sample files =="
16
+ # unless File.exist?("config/database.yml")
17
+ # system "cp config/database.yml.sample config/database.yml"
18
+ # end
19
+
20
+ puts "\n== Preparing database =="
21
+ system "bin/rake db:setup"
22
+
23
+ puts "\n== Removing old logs and tempfiles =="
24
+ system "rm -f log/*"
25
+ system "rm -rf tmp/cache"
26
+
27
+ puts "\n== Restarting application server =="
28
+ system "touch tmp/restart.txt"
29
+ end
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,32 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ # Pick the frameworks you want:
4
+ require "active_record/railtie"
5
+ require "action_controller/railtie"
6
+ require "action_mailer/railtie"
7
+ require "action_view/railtie"
8
+ require "sprockets/railtie"
9
+ # require "rails/test_unit/railtie"
10
+
11
+ Bundler.require(*Rails.groups)
12
+ require "preserve"
13
+
14
+ module Dummy
15
+ class Application < Rails::Application
16
+ # Settings in config/environments/* take precedence over those specified here.
17
+ # Application configuration should go into files in config/initializers
18
+ # -- all .rb files in that directory are automatically loaded.
19
+
20
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
21
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
22
+ # config.time_zone = 'Central Time (US & Canada)'
23
+
24
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
25
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
26
+ # config.i18n.default_locale = :de
27
+
28
+ # Do not swallow errors in after_commit/after_rollback callbacks.
29
+ config.active_record.raise_in_transactional_callbacks = true
30
+ end
31
+ end
32
+