user_settings 0.0.1

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 (55) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +17 -0
  3. data/.travis.yml +8 -0
  4. data/Appraisals +11 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +135 -0
  8. data/Rakefile +17 -0
  9. data/app/assets/javascripts/user_settings.js +78 -0
  10. data/app/controllers/concerns/user_settings_concern.rb +19 -0
  11. data/app/controllers/user_settings_controller.rb +37 -0
  12. data/app/helpers/user_settings_helper.rb +16 -0
  13. data/app/models/user_settings/key.rb +30 -0
  14. data/app/presenters/user_settings_presenter.rb +18 -0
  15. data/config/routes.rb +8 -0
  16. data/lib/user_settings/engine.rb +13 -0
  17. data/lib/user_settings/route_drawers/default.rb +38 -0
  18. data/lib/user_settings/version.rb +3 -0
  19. data/lib/user_settings.rb +44 -0
  20. data/spec/controllers/user_settings_controller_spec.rb +173 -0
  21. data/spec/dummy/Rakefile +7 -0
  22. data/spec/dummy/app/assets/images/.keep +0 -0
  23. data/spec/dummy/app/assets/javascripts/application.js +16 -0
  24. data/spec/dummy/app/assets/javascripts/welcome.js.coffee +3 -0
  25. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  26. data/spec/dummy/app/assets/stylesheets/welcome.css.scss +3 -0
  27. data/spec/dummy/app/controllers/application_controller.rb +12 -0
  28. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  29. data/spec/dummy/app/controllers/welcome_controller.rb +4 -0
  30. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  31. data/spec/dummy/app/helpers/welcome_helper.rb +2 -0
  32. data/spec/dummy/app/models/.keep +0 -0
  33. data/spec/dummy/app/models/concerns/.keep +0 -0
  34. data/spec/dummy/app/models/user.rb +3 -0
  35. data/spec/dummy/app/views/layouts/application.html.erb +16 -0
  36. data/spec/dummy/app/views/welcome/index.html.erb +2 -0
  37. data/spec/dummy/config/application.rb +16 -0
  38. data/spec/dummy/config/boot.rb +9 -0
  39. data/spec/dummy/config/environment.rb +5 -0
  40. data/spec/dummy/config/environments/test.rb +31 -0
  41. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  42. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  43. data/spec/dummy/config/initializers/inflections.rb +16 -0
  44. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  45. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  46. data/spec/dummy/config/initializers/session_store.rb +3 -0
  47. data/spec/dummy/config/initializers/usettings.rb +8 -0
  48. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  49. data/spec/dummy/config/locales/en.yml +23 -0
  50. data/spec/dummy/config/routes.rb +2 -0
  51. data/spec/dummy/config.ru +4 -0
  52. data/spec/routing/routes_spec.rb +89 -0
  53. data/spec/spec_helper.rb +19 -0
  54. data/user_settings.gemspec +33 -0
  55. metadata +285 -0
data/config/routes.rb ADDED
@@ -0,0 +1,8 @@
1
+ Rails.application.routes.draw do
2
+ if UserSettings.redis
3
+ get UserSettings.route_drawer.match_get_settings
4
+ post UserSettings.route_drawer.match_set_settings
5
+ put UserSettings.route_drawer.match_set_once_settings
6
+ delete UserSettings.route_drawer.match_remove_settings
7
+ end
8
+ end
@@ -0,0 +1,13 @@
1
+ module UserSettings
2
+ class Engine < Rails::Engine
3
+ initializer 'Require concerns path' do |app|
4
+ concerns_path = 'app/controllers/concerns'
5
+
6
+ unless app.paths.keys.include?(concerns_path)
7
+ app.paths.add(concerns_path)
8
+ end
9
+
10
+ require 'concerns/user_settings_concern'
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,38 @@
1
+ # Similar to https://github.com/thoughtbot/high_voltage/blob/master/lib/high_voltage.rb
2
+ module UserSettings
3
+ module RouteDrawers
4
+ class Default
5
+ def self.match_get_settings
6
+ {
7
+ "/#{UserSettings.base_path}:key" => 'user_settings#show',
8
+ :as => :get_user_settings,
9
+ :format => :json
10
+ }
11
+ end
12
+
13
+ def self.match_set_once_settings
14
+ {
15
+ "/#{UserSettings.base_path}:key" => 'user_settings#create_once',
16
+ :as => :set_once_user_settings,
17
+ :format => :json
18
+ }
19
+ end
20
+
21
+ def self.match_set_settings
22
+ {
23
+ "/#{UserSettings.base_path}:key" => 'user_settings#create',
24
+ :as => :set_user_settings,
25
+ :format => :json
26
+ }
27
+ end
28
+
29
+ def self.match_remove_settings
30
+ {
31
+ "/#{UserSettings.base_path}:key" => 'user_settings#destroy',
32
+ :as => :remove_user_settings,
33
+ :format => :json
34
+ }
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module UserSettings
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,44 @@
1
+ require "user_settings/version"
2
+ require "user_settings/route_drawers/default"
3
+
4
+ require 'active_support/core_ext/module/attribute_accessors'
5
+ require 'active_support/core_ext'
6
+
7
+ module UserSettings
8
+
9
+ # Redis options are
10
+ # * host
11
+ # * port
12
+ # * db
13
+ # * redis_connection - Give a connection directly
14
+ #
15
+ mattr_accessor :redis_options
16
+ @@redis_options = false
17
+ @@redis_connection = nil
18
+
19
+ mattr_accessor :base_path
20
+ @@base_path = 'usettings/'
21
+
22
+ mattr_accessor :route_drawer
23
+ @@route_drawer = UserSettings::RouteDrawers::Default
24
+
25
+ mattr_accessor :expiration_time
26
+ @@expiration_time = 3.months
27
+
28
+ def self.configure
29
+ yield self
30
+ end
31
+
32
+ def self.redis
33
+ if @@redis_options
34
+ @@redis_connection ||= @@redis_options.delete(:redis_connection)
35
+ if @@redis_connection == nil
36
+ @@redis_connection ||= Redis.new({:host => 'localhost', :port => 6379, :db => 1}.merge(@@redis_options))
37
+ end
38
+ end
39
+ @@redis_connection
40
+ end
41
+
42
+ require 'user_settings/engine' if defined?(Rails)
43
+
44
+ end
@@ -0,0 +1,173 @@
1
+ require 'spec_helper'
2
+ require 'json'
3
+
4
+ describe UserSettingsController do
5
+
6
+ context 'with no current user' do
7
+
8
+ before do
9
+ ApplicationController.any_instance.stub(:set_current_user) { true }
10
+ end
11
+
12
+ context 'for get key' do
13
+ before { get :show, key: :test, format: :json }
14
+
15
+ it 'returns failure' do
16
+ response.should_not be_success
17
+ end
18
+
19
+ end
20
+
21
+ context 'for set key' do
22
+ before { post :create, key: :test, value: :test, format: :json }
23
+
24
+ it 'returns failure' do
25
+ response.should_not be_success
26
+ end
27
+
28
+ end
29
+
30
+ context 'for set once key' do
31
+ before { put :create_once, key: :test, value: :test, format: :json }
32
+
33
+ it 'returns failure' do
34
+ response.should_not be_success
35
+ end
36
+
37
+ end
38
+
39
+ context 'for delete key' do
40
+ before { delete :destroy, key: :test, format: :json }
41
+
42
+ it 'returns failure' do
43
+ response.should_not be_success
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
50
+ context 'with current user' do
51
+
52
+ let(:value) { 'test_value' }
53
+
54
+ context 'for get key' do
55
+ context 'when no value exists' do
56
+ before { get :show, key: :test, format: :json }
57
+
58
+ it 'returns success' do
59
+ response.should be_success
60
+ end
61
+
62
+ it 'should not find the key' do
63
+ parsed_body = JSON.parse(response.body)
64
+ parsed_body['success'].should == false
65
+ end
66
+ end
67
+
68
+ context 'when a value exists' do
69
+ before do
70
+ UserSettings::Key.stub(:find) { value }
71
+ get :show, key: :test, format: :json
72
+ end
73
+
74
+ it 'returns success' do
75
+ response.should be_success
76
+ end
77
+
78
+ it 'should find the key' do
79
+ parsed_body = JSON.parse(response.body)
80
+ parsed_body['value'].should == value
81
+ end
82
+ end
83
+ end
84
+
85
+ context 'for set key' do
86
+
87
+ before do
88
+ UserSettings::Key.stub(:create_or_update) { 'OK' }
89
+ end
90
+
91
+ context 'when no value exists' do
92
+ before do
93
+ UserSettings::Key.stub(:find) { false }
94
+ post :create, key: :test, value: :test, format: :json
95
+ end
96
+
97
+ it 'returns success' do
98
+ response.should be_success
99
+ end
100
+
101
+ it 'should have success in json' do
102
+ parsed_body = JSON.parse(response.body)
103
+ parsed_body['success'].should == true
104
+ end
105
+ end
106
+
107
+ context 'when value exists' do
108
+ before do
109
+ UserSettings::Key.stub(:find) { true }
110
+ post :create, key: :test, value: :test, format: :json
111
+ end
112
+
113
+ it 'returns success' do
114
+ response.should be_success
115
+ end
116
+
117
+ it 'should have success in json' do
118
+ parsed_body = JSON.parse(response.body)
119
+ parsed_body['success'].should == true
120
+ end
121
+ end
122
+
123
+ end
124
+
125
+ context 'for set once key' do
126
+
127
+ before do
128
+ UserSettings::Key.stub(:create_or_update) { 'OK' }
129
+ end
130
+
131
+ context 'when no value exists' do
132
+ before do
133
+ UserSettings::Key.stub(:find) { false }
134
+ put :create_once, key: :test, value: :test, format: :json
135
+ end
136
+
137
+ it 'returns success' do
138
+ response.should be_success
139
+ end
140
+
141
+ it 'should have success in json' do
142
+ parsed_body = JSON.parse(response.body)
143
+ parsed_body['success'].should == true
144
+ end
145
+ end
146
+
147
+ context 'when value exists' do
148
+ before do
149
+ UserSettings::Key.stub(:find) { true }
150
+ put :create_once, key: :test, value: :test, format: :json
151
+ end
152
+
153
+ it 'returns success' do
154
+ response.should be_success
155
+ end
156
+
157
+ it 'should have success in json' do
158
+ parsed_body = JSON.parse(response.body)
159
+ parsed_body['success'].should == false
160
+ end
161
+ end
162
+ end
163
+
164
+ context 'for delete key' do
165
+ before { delete :destroy, key: :test, format: :json }
166
+
167
+ it 'returns success' do
168
+ response.should be_success
169
+ end
170
+
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,7 @@
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
+ require 'rake'
6
+
7
+ Dummy::Application.load_tasks
File without changes
@@ -0,0 +1,16 @@
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 vendor/assets/javascripts of plugins, if any, 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/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require turbolinks
16
+ //= require_tree .
@@ -0,0 +1,3 @@
1
+ # Place all the behaviors and hooks related to the matching controller here.
2
+ # All this logic will automatically be available in application.js.
3
+ # You can use CoffeeScript in this file: http://coffeescript.org/
@@ -0,0 +1,13 @@
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 vendor/assets/stylesheets of plugins, if any, 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 top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,3 @@
1
+ // Place all the styles related to the welcome controller here.
2
+ // They will automatically be included in application.css.
3
+ // You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,12 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+
6
+ before_filter :set_current_user
7
+
8
+ private
9
+ def set_current_user
10
+ @current_user = User.new('123456', SecureRandom.uuid)
11
+ end
12
+ end
File without changes
@@ -0,0 +1,4 @@
1
+ class WelcomeController < ApplicationController
2
+ def index
3
+ end
4
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module WelcomeHelper
2
+ end
File without changes
File without changes
@@ -0,0 +1,3 @@
1
+ class User < Struct.new(:id, :uuid)
2
+
3
+ end
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>UsettingsTest</title>
5
+ <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6
+ <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7
+ <%= javascript_include_tag_for_user_settings %>
8
+ <%= csrf_meta_tags %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ <%= init_user_settings %>
15
+ </body>
16
+ </html>
@@ -0,0 +1,2 @@
1
+ <h1>Welcome#index</h1>
2
+ <p>Find me in app/views/welcome/index.html.erb</p>
@@ -0,0 +1,16 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require "active_model/railtie"
4
+ require "action_controller/railtie"
5
+ require "action_view/railtie"
6
+
7
+ Bundler.require(:default, Rails.env)
8
+ require "user_settings"
9
+
10
+ module Dummy
11
+ class Application < Rails::Application
12
+ # For Ruby 1.9
13
+ config.encoding = "utf-8"
14
+ config.secret_key_base = 'Testme'
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ gemfile = File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ if File.exist?(gemfile)
5
+ ENV['BUNDLE_GEMFILE'] = gemfile
6
+ require 'bundler/setup'
7
+ end
8
+
9
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Dummy::Application.initialize!
@@ -0,0 +1,31 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb.
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ # Do not eager load code on boot. This avoids loading your whole application
11
+ # just for the purpose of running a single test. If you are using a tool that
12
+ # preloads Rails for running tests, you may have to set it to true.
13
+ config.eager_load = false
14
+
15
+ # Configure static asset server for tests with Cache-Control for performance.
16
+ config.serve_static_assets = true
17
+ config.static_cache_control = "public, max-age=3600"
18
+
19
+ # Show full error reports and disable caching.
20
+ config.consider_all_requests_local = true
21
+ config.action_controller.perform_caching = false
22
+
23
+ # Raise exceptions instead of rendering exception templates.
24
+ config.action_dispatch.show_exceptions = false
25
+
26
+ # Disable request forgery protection in test environment.
27
+ config.action_controller.allow_forgery_protection = false
28
+
29
+ # Print deprecation notices to the stderr.
30
+ config.active_support.deprecation = :stderr
31
+ end