easy_tokens 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +34 -0
  5. data/app/controllers/easy_tokens/application_controller.rb +4 -0
  6. data/app/controllers/easy_tokens/tokens_controller.rb +62 -0
  7. data/app/models/easy_tokens/token.rb +12 -0
  8. data/app/views/easy_tokens/tokens/_form.html.erb +21 -0
  9. data/app/views/easy_tokens/tokens/edit.html.erb +6 -0
  10. data/app/views/easy_tokens/tokens/index.html.erb +30 -0
  11. data/app/views/easy_tokens/tokens/new.html.erb +5 -0
  12. data/app/views/easy_tokens/tokens/show.html.erb +24 -0
  13. data/app/views/layouts/easy_tokens/application.html.erb +14 -0
  14. data/config/routes.rb +4 -0
  15. data/db/migrate/20141103194654_create_easy_tokens_tokens.rb +12 -0
  16. data/lib/easy_tokens.rb +13 -0
  17. data/lib/easy_tokens/authorization.rb +30 -0
  18. data/lib/easy_tokens/engine.rb +11 -0
  19. data/lib/easy_tokens/token_validator.rb +7 -0
  20. data/lib/easy_tokens/version.rb +3 -0
  21. data/test/controllers/easy_tokens/tokens_controller_test.rb +53 -0
  22. data/test/dummy/README.rdoc +28 -0
  23. data/test/dummy/Rakefile +6 -0
  24. data/test/dummy/app/assets/javascripts/application.js +13 -0
  25. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  26. data/test/dummy/app/controllers/application_controller.rb +5 -0
  27. data/test/dummy/app/helpers/application_helper.rb +2 -0
  28. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  29. data/test/dummy/bin/bundle +3 -0
  30. data/test/dummy/bin/rails +4 -0
  31. data/test/dummy/bin/rake +4 -0
  32. data/test/dummy/config.ru +4 -0
  33. data/test/dummy/config/application.rb +23 -0
  34. data/test/dummy/config/boot.rb +5 -0
  35. data/test/dummy/config/database.yml +25 -0
  36. data/test/dummy/config/environment.rb +5 -0
  37. data/test/dummy/config/environments/development.rb +37 -0
  38. data/test/dummy/config/environments/production.rb +78 -0
  39. data/test/dummy/config/environments/test.rb +39 -0
  40. data/test/dummy/config/initializers/assets.rb +8 -0
  41. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  42. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  43. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  44. data/test/dummy/config/initializers/inflections.rb +16 -0
  45. data/test/dummy/config/initializers/mime_types.rb +4 -0
  46. data/test/dummy/config/initializers/session_store.rb +3 -0
  47. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  48. data/test/dummy/config/locales/en.yml +23 -0
  49. data/test/dummy/config/routes.rb +3 -0
  50. data/test/dummy/config/secrets.yml +22 -0
  51. data/test/dummy/db/development.sqlite3 +0 -0
  52. data/test/dummy/db/schema.rb +25 -0
  53. data/test/dummy/db/test.sqlite3 +0 -0
  54. data/test/dummy/log/development.log +382 -0
  55. data/test/dummy/log/test.log +2557 -0
  56. data/test/dummy/public/404.html +67 -0
  57. data/test/dummy/public/422.html +67 -0
  58. data/test/dummy/public/500.html +66 -0
  59. data/test/dummy/public/favicon.ico +0 -0
  60. data/test/easy_tokens_test.rb +7 -0
  61. data/test/fixtures/easy_tokens/tokens.yml +9 -0
  62. data/test/helpers/easy_tokens/tokens_helper_test.rb +6 -0
  63. data/test/integration/navigation_test.rb +10 -0
  64. data/test/models/easy_tokens/token_test.rb +13 -0
  65. data/test/test_helper.rb +28 -0
  66. metadata +197 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 93427b852f798c78bd0597c2018a62b800cdd8f5
4
+ data.tar.gz: b96504e2a49c153a313ba6fb76ff7fd6791d6f52
5
+ SHA512:
6
+ metadata.gz: 09b9558fc463ca7646340638653ffd5830da1a79b51b0aa7ed11285c5125e734f6b48437c5f39767a4e29e9bbf300f4faa2a2d5416ece5b0f1020e7e846a37aa
7
+ data.tar.gz: 1d4356ab78343119887759882d53b54d94d564f44ee7c2f4aefb8be2d8b3764ba2221c75e075ec1de5b359cfadd74234e9c7623d21600bd9317626f31283cad1
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
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,3 @@
1
+ = EasyTokens
2
+
3
+ This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'EasyTokens'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,4 @@
1
+ module EasyTokens
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,62 @@
1
+ require_dependency 'easy_tokens/application_controller'
2
+
3
+ module EasyTokens
4
+ class TokensController < ::ApplicationController
5
+ before_action :authorize!
6
+ before_action :set_token, only: [:show, :edit, :update, :destroy]
7
+
8
+ layout false
9
+
10
+ def index
11
+ @tokens = Token.all
12
+ end
13
+
14
+ def show
15
+ end
16
+
17
+ def new
18
+ @token = Token.new
19
+ end
20
+
21
+ def edit
22
+ end
23
+
24
+ def create
25
+ @token = Token.new(token_params)
26
+ @token.owner_id = owner_resource.id
27
+
28
+ if @token.save
29
+ redirect_to @token, notice: 'Token was successfully created.'
30
+ else
31
+ render :new
32
+ end
33
+ end
34
+
35
+ def update
36
+ if @token.update(token_params)
37
+ redirect_to @token, notice: 'Token was successfully updated.'
38
+ else
39
+ render :edit
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def set_token
46
+ @token = Token.find(params[:id])
47
+ end
48
+
49
+ def token_params
50
+ params.require(:token).permit(:description)
51
+ end
52
+
53
+ def authorize!
54
+ return if owner_resource.public_send EasyTokens.owner_authorization_method
55
+ render text: 'Unauthorized', status: :unauthorized
56
+ end
57
+
58
+ def owner_resource
59
+ @owner_resource ||= send EasyTokens.token_owner_method
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,12 @@
1
+ module EasyTokens
2
+ class Token < ActiveRecord::Base
3
+ after_initialize :ensure_token_presence
4
+ belongs_to :owner, class_name: EasyTokens.token_owner_class
5
+
6
+ private
7
+
8
+ def ensure_token_presence
9
+ self.value ||= SecureRandom.hex(16)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,21 @@
1
+ <%= form_for(@token) do |f| %>
2
+ <% if @token.errors.any? %>
3
+ <div id="error_explanation">
4
+ <h2><%= pluralize(@token.errors.count, "error") %> prohibited this token from being saved:</h2>
5
+
6
+ <ul>
7
+ <% @token.errors.full_messages.each do |message| %>
8
+ <li><%= message %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+
14
+ <div class="field">
15
+ <%= f.label :description %><br>
16
+ <%= f.text_field :description %>
17
+ </div>
18
+ <div class="actions">
19
+ <%= f.submit %>
20
+ </div>
21
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <h1>Editing token</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Show', @token %> |
6
+ <%= link_to 'Back', tokens_path %>
@@ -0,0 +1,30 @@
1
+ <h1>Listing tokens</h1>
2
+
3
+ <table>
4
+ <thead>
5
+ <tr>
6
+ <th>Value</th>
7
+ <th>Description</th>
8
+ <th>Owner</th>
9
+ <th>Deactivated at</th>
10
+ <th colspan="3"></th>
11
+ </tr>
12
+ </thead>
13
+
14
+ <tbody>
15
+ <% @tokens.each do |token| %>
16
+ <tr>
17
+ <td><%= token.value %></td>
18
+ <td><%= token.description %></td>
19
+ <td><%= token.owner %></td>
20
+ <td><%= token.deactivated_at %></td>
21
+ <td><%= link_to 'Show', token %></td>
22
+ <td><%= link_to 'Edit', edit_token_path(token) %></td>
23
+ </tr>
24
+ <% end %>
25
+ </tbody>
26
+ </table>
27
+
28
+ <br>
29
+
30
+ <%= link_to 'New Token', new_token_path %>
@@ -0,0 +1,5 @@
1
+ <h1>New token</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Back', tokens_path %>
@@ -0,0 +1,24 @@
1
+ <p id="notice"><%= notice %></p>
2
+
3
+ <p>
4
+ <strong>Value:</strong>
5
+ <%= @token.value %>
6
+ </p>
7
+
8
+ <p>
9
+ <strong>Description:</strong>
10
+ <%= @token.description %>
11
+ </p>
12
+
13
+ <p>
14
+ <strong>Owner:</strong>
15
+ <%= @token.owner %>
16
+ </p>
17
+
18
+ <p>
19
+ <strong>Deactivated at:</strong>
20
+ <%= @token.deactivated_at %>
21
+ </p>
22
+
23
+ <%= link_to 'Edit', edit_token_path(@token) %> |
24
+ <%= link_to 'Back', tokens_path %>
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>EasyTokens</title>
5
+ <%= stylesheet_link_tag "easy_tokens/application", media: "all" %>
6
+ <%= javascript_include_tag "easy_tokens/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,4 @@
1
+ EasyTokens::Engine.routes.draw do
2
+ root to: 'tokens#index'
3
+ resources :tokens, except: [:destroy]
4
+ end
@@ -0,0 +1,12 @@
1
+ class CreateEasyTokensTokens < ActiveRecord::Migration
2
+ def change
3
+ create_table :easy_tokens_tokens do |t|
4
+ t.string :value
5
+ t.string :description
6
+ t.string :owner_id
7
+ t.datetime :deactivated_at
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ require "easy_tokens/engine"
2
+
3
+ module EasyTokens
4
+ mattr_accessor :token_owner_method do
5
+ :current_user
6
+ end
7
+ mattr_accessor :token_owner_class do
8
+ 'User'
9
+ end
10
+ mattr_accessor :owner_authorization_method do
11
+ 'admin?'
12
+ end
13
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'token_validator'
2
+
3
+ module EasyTokens
4
+ module Authorization
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def check_token_on(*action_names)
9
+ before_filter :validate_token, only: action_names
10
+ end
11
+ end
12
+
13
+ def validate_token
14
+ if params[:token].present?
15
+ return if TokenValidator.valid?(params[:token])
16
+ token_invalid and return
17
+ else
18
+ token_required
19
+ end
20
+ end
21
+
22
+ def token_required
23
+ render text: 'Token required', status: :bad_request
24
+ end
25
+
26
+ def token_invalid
27
+ render text: 'Token is invalid', status: :forbidden
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'authorization'
2
+
3
+ module EasyTokens
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace EasyTokens
6
+
7
+ initializer 'easy_tokens.controller_extensions' do
8
+ ActionController::Base.send(:include, EasyTokens::Authorization)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ module EasyTokens
2
+ class TokenValidator
3
+ def self.valid?(token)
4
+ Token.exists?(value: token, deactivated_at: nil)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module EasyTokens
2
+ VERSION = "0.3.0"
3
+ end
@@ -0,0 +1,53 @@
1
+ require 'test_helper'
2
+
3
+ module EasyTokens
4
+ class TokensControllerTest < ActionController::TestCase
5
+ setup do
6
+ @token = easy_tokens_tokens(:one)
7
+ class User
8
+ def id
9
+ 1
10
+ end
11
+
12
+ def admin?
13
+ true
14
+ end
15
+ end
16
+ @controller.instance_variable_set(:@owner_resource, User.new)
17
+ end
18
+
19
+ test "should get index" do
20
+ get :index
21
+ assert_response :success
22
+ assert_not_nil assigns(:tokens)
23
+ end
24
+
25
+ test "should get new" do
26
+ get :new
27
+ assert_response :success
28
+ end
29
+
30
+ test "should create token" do
31
+ assert_difference('Token.count') do
32
+ post :create, token: { deactivated_at: @token.deactivated_at, description: @token.description, owner_id: @token.owner_id, value: @token.value }
33
+ end
34
+
35
+ assert_redirected_to token_path(assigns(:token))
36
+ end
37
+
38
+ test "should show token" do
39
+ get :show, id: @token
40
+ assert_response :success
41
+ end
42
+
43
+ test "should get edit" do
44
+ get :edit, id: @token
45
+ assert_response :success
46
+ end
47
+
48
+ test "should update token" do
49
+ patch :update, id: @token, token: { deactivated_at: @token.deactivated_at, description: @token.description, owner_id: @token.owner_id, value: @token.value }
50
+ assert_redirected_to token_path(assigns(:token))
51
+ end
52
+ end
53
+ 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