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.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/.travis.yml +8 -0
- data/Appraisals +11 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +135 -0
- data/Rakefile +17 -0
- data/app/assets/javascripts/user_settings.js +78 -0
- data/app/controllers/concerns/user_settings_concern.rb +19 -0
- data/app/controllers/user_settings_controller.rb +37 -0
- data/app/helpers/user_settings_helper.rb +16 -0
- data/app/models/user_settings/key.rb +30 -0
- data/app/presenters/user_settings_presenter.rb +18 -0
- data/config/routes.rb +8 -0
- data/lib/user_settings/engine.rb +13 -0
- data/lib/user_settings/route_drawers/default.rb +38 -0
- data/lib/user_settings/version.rb +3 -0
- data/lib/user_settings.rb +44 -0
- data/spec/controllers/user_settings_controller_spec.rb +173 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/images/.keep +0 -0
- data/spec/dummy/app/assets/javascripts/application.js +16 -0
- data/spec/dummy/app/assets/javascripts/welcome.js.coffee +3 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/assets/stylesheets/welcome.css.scss +3 -0
- data/spec/dummy/app/controllers/application_controller.rb +12 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/controllers/welcome_controller.rb +4 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/helpers/welcome_helper.rb +2 -0
- data/spec/dummy/app/models/.keep +0 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/models/user.rb +3 -0
- data/spec/dummy/app/views/layouts/application.html.erb +16 -0
- data/spec/dummy/app/views/welcome/index.html.erb +2 -0
- data/spec/dummy/config/application.rb +16 -0
- data/spec/dummy/config/boot.rb +9 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/test.rb +31 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +12 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/usettings.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +2 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/routing/routes_spec.rb +89 -0
- data/spec/spec_helper.rb +19 -0
- data/user_settings.gemspec +33 -0
- metadata +285 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YmQ2NTljMjc0NjVmN2NiMTFjYjE4NDZiOTE2MzlmYzgxNTkyNWMxNA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NGJhZDBjMzg5ODA2YmRhZDA4OWYwMWZjOTIyZjdlODgwZDc4ZGUxZQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NTU5NTRhMGJhN2JmZmVhYTc3MTNiNjMzMTdlYTkzZGU1ODEwMzQ2N2EwODJi
|
10
|
+
NzA0NmYwMWYxZTk2ZDk2NTk2YmNjNDJlZWUxYTI2ZDk3MzZlNWZlYWQ0ZWY2
|
11
|
+
ZTU4NTY0Nzg3MDExNDBhMTRjODMwMDM4M2NlNGM2MmNhYTM4MWY=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MTc1YzExY2I2Y2NmNDRlYmYxMTk5ZTUyYzM3ZTIyOTQ0MDUyMTQxZjA2ZWZk
|
14
|
+
YjkxZGEyMzk3NzZmMzAxMWI0MWRjZmMzZjM4NDZmNjQyZTE1YTZhNTkwYTYz
|
15
|
+
NjdmM2VkOTI1MzQ1MWNhZWI5YjlhMzcxYzlhYzU4OTBjMTRlY2M=
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Appraisals
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
if RUBY_VERSION >= '2.0'
|
2
|
+
rails_versions = ['~> 3.2.13', '~> 4.0.0']
|
3
|
+
else
|
4
|
+
rails_versions = ['~> 3.1.12', '~> 3.2.13']
|
5
|
+
end
|
6
|
+
|
7
|
+
rails_versions.each do |rails_version|
|
8
|
+
appraise "rails#{rails_version.slice(/\d+\.\d+/)}" do
|
9
|
+
gem 'rails', rails_version
|
10
|
+
end
|
11
|
+
end
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Dinesh Vasudevan
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
# UserSettings
|
2
|
+
|
3
|
+
[](https://codeclimate.com/github/dinks/user_settings)
|
4
|
+
|
5
|
+
[](https://travis-ci.org/dinks/user_settings)
|
6
|
+
|
7
|
+
To save user based settings to redis, retrieve them and delete them.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'user_settings'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install user_settings
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
The gem requires a Redis object to be passed for configuration as it stores the key:value in Redis
|
26
|
+
|
27
|
+
### Configuration
|
28
|
+
|
29
|
+
Create an initializer with configuration
|
30
|
+
|
31
|
+
require 'user_settings'
|
32
|
+
|
33
|
+
UserSettings.configure do |c|
|
34
|
+
|
35
|
+
redis = Redis.new({ host: "127.0.0.1", port: 6379, db: 0 })
|
36
|
+
|
37
|
+
c.redis_options = {redis_connection: redis} # Set the redis instance
|
38
|
+
c.base_path = 'my_custom_base_path/' # Set a custom base path. 'usettings/' is default
|
39
|
+
c.route_drawer = MyCustomClass # See UserSettings::RouteDrawers::Default
|
40
|
+
c.expiration_time = 2.months # Change the expiration time. 3.months is default
|
41
|
+
end
|
42
|
+
|
43
|
+
Redis could also be configured by setting the `redis_options` like
|
44
|
+
|
45
|
+
{ host: 'localhost', port: 6379, db: 1 }
|
46
|
+
|
47
|
+
The default routes are
|
48
|
+
|
49
|
+
get_user_settings GET /usettings/:key(.:format) user_settings#show
|
50
|
+
set_user_settings POST /usettings/:key(.:format) user_settings#create
|
51
|
+
set_once_user_settings PUT /usettings/:key(.:format) user_settings#create_once
|
52
|
+
remove_user_settings DELETE /usettings/:key(.:format) user_settings#destroy
|
53
|
+
|
54
|
+
The storage is with respect to user and requires a conventional `@current_user` object with an
|
55
|
+
attribute `id` present. :|
|
56
|
+
|
57
|
+
2 JavaScripts are to be included in the layout
|
58
|
+
|
59
|
+
For the main javascript file. You might want to add this to the `config.assets.precompile` in `production.rb`
|
60
|
+
|
61
|
+
<%= javascript_include_tag_for_user_settings %>
|
62
|
+
|
63
|
+
The user settings are to be initialized with your custom settings in places where you would use it
|
64
|
+
|
65
|
+
<%= init_user_settings %>
|
66
|
+
|
67
|
+
|
68
|
+
### How to use ?
|
69
|
+
|
70
|
+
The JavaScript used Promises and jQuery's Deferred which *emulates* Promises. Need to think about this more!
|
71
|
+
|
72
|
+
To get a key
|
73
|
+
|
74
|
+
$.userSettings.
|
75
|
+
get('test').
|
76
|
+
then(function(d) {
|
77
|
+
// The response will have
|
78
|
+
// - success -> which is either true or false
|
79
|
+
// - value -> value of the key if the success is true
|
80
|
+
console.log(d);
|
81
|
+
}).
|
82
|
+
fail(function(f) {
|
83
|
+
// The response will have
|
84
|
+
// - status -> 401 if unauthorized
|
85
|
+
});
|
86
|
+
|
87
|
+
To Set a key with a Value
|
88
|
+
|
89
|
+
$.userSettings.
|
90
|
+
set('test', 'two').
|
91
|
+
then(function(d) {
|
92
|
+
// The response will have
|
93
|
+
// - success -> which is either true or false
|
94
|
+
// - value -> "OK" if success is true
|
95
|
+
console.log(d);
|
96
|
+
}).
|
97
|
+
fail(function(f) {
|
98
|
+
// The response will have
|
99
|
+
// - status -> 401 if unauthorized
|
100
|
+
});
|
101
|
+
|
102
|
+
To Set a key with a Value *ONCE*
|
103
|
+
|
104
|
+
$.userSettings.
|
105
|
+
setOnce('test', 'two').
|
106
|
+
then(function(d) {
|
107
|
+
// The response will have
|
108
|
+
// - success -> which is either true or false. It is true if the set is for the first time
|
109
|
+
// - value -> "OK" if success is true
|
110
|
+
console.log(d);
|
111
|
+
}).
|
112
|
+
fail(function(f) {
|
113
|
+
// The response will have
|
114
|
+
// - status -> 401 if unauthorized
|
115
|
+
});
|
116
|
+
|
117
|
+
To Remove a key:value
|
118
|
+
|
119
|
+
$.userSettings.
|
120
|
+
remove('test').
|
121
|
+
then(function(d) {
|
122
|
+
// There will not be any call to then !!
|
123
|
+
}).
|
124
|
+
fail(function(f) {
|
125
|
+
// The response will have
|
126
|
+
// - status -> 401 if unauthorized
|
127
|
+
});
|
128
|
+
|
129
|
+
## Contributing
|
130
|
+
|
131
|
+
1. Fork it
|
132
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
133
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
134
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
135
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
|
5
|
+
require 'rake'
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
require 'appraisal'
|
8
|
+
|
9
|
+
RSpec::Core::RakeTask.new(:spec)
|
10
|
+
|
11
|
+
desc 'Default'
|
12
|
+
task :default => [:all]
|
13
|
+
|
14
|
+
desc 'Test the engine under all supported Rails versions'
|
15
|
+
task all: ['appraisal:install'] do |t|
|
16
|
+
exec 'rake appraisal spec'
|
17
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
/* global $, jQuery */
|
2
|
+
|
3
|
+
(function($) {
|
4
|
+
$.userSettings = function(url) {
|
5
|
+
var baseUrl = url;
|
6
|
+
|
7
|
+
var executor = function(options) {
|
8
|
+
var deferred = new $.Deferred();
|
9
|
+
|
10
|
+
if ($.isFunction(options.callback)) {
|
11
|
+
deferred.then(options.callback);
|
12
|
+
}
|
13
|
+
|
14
|
+
var dataToSend = $.extend({
|
15
|
+
t: Date.now()
|
16
|
+
}, options.params);
|
17
|
+
|
18
|
+
$.ajax({
|
19
|
+
url: baseUrl + options.key,
|
20
|
+
dataType: 'json',
|
21
|
+
type: options.requestType,
|
22
|
+
data: dataToSend
|
23
|
+
}).then(function(data) {
|
24
|
+
deferred.resolve(data);
|
25
|
+
}).fail(function(data){
|
26
|
+
deferred.reject(data);
|
27
|
+
});
|
28
|
+
|
29
|
+
return deferred.promise();
|
30
|
+
};
|
31
|
+
|
32
|
+
// $.userSettings.get('test').then(function(a){console.log(a);}).fail(function(f){console.log(f);});
|
33
|
+
var get = function(key, callback) {
|
34
|
+
return executor({
|
35
|
+
key: key,
|
36
|
+
requestType: 'GET',
|
37
|
+
callback: callback
|
38
|
+
});
|
39
|
+
};
|
40
|
+
|
41
|
+
var set = function(key, value, callback) {
|
42
|
+
return executor({
|
43
|
+
key: key,
|
44
|
+
params: {
|
45
|
+
value: value
|
46
|
+
},
|
47
|
+
requestType: 'POST',
|
48
|
+
callback: callback
|
49
|
+
});
|
50
|
+
};
|
51
|
+
|
52
|
+
var setOnce = function(key, value, callback) {
|
53
|
+
return executor({
|
54
|
+
key: key,
|
55
|
+
params: {
|
56
|
+
value: value
|
57
|
+
},
|
58
|
+
requestType: 'PUT',
|
59
|
+
callback: callback
|
60
|
+
});
|
61
|
+
};
|
62
|
+
|
63
|
+
var remove = function(key, callback) {
|
64
|
+
return executor({
|
65
|
+
key: key,
|
66
|
+
requestType: 'DELETE',
|
67
|
+
callback: callback
|
68
|
+
});
|
69
|
+
};
|
70
|
+
|
71
|
+
return {
|
72
|
+
get: get,
|
73
|
+
set: set,
|
74
|
+
setOnce: setOnce,
|
75
|
+
remove: remove
|
76
|
+
};
|
77
|
+
};
|
78
|
+
})(jQuery);
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module UserSettings::UserSettingsConcern
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
|
6
|
+
layout false
|
7
|
+
|
8
|
+
before_filter :check_if_authorized
|
9
|
+
|
10
|
+
private
|
11
|
+
def check_if_authorized
|
12
|
+
unless @current_user
|
13
|
+
head :unauthorized
|
14
|
+
false
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class UserSettingsController < ApplicationController
|
2
|
+
include UserSettings::UserSettingsConcern
|
3
|
+
|
4
|
+
def show
|
5
|
+
render json: create_presenter(:find).as_json
|
6
|
+
end
|
7
|
+
|
8
|
+
def create
|
9
|
+
render json: create_presenter(:create).as_json
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_once
|
13
|
+
render json: create_presenter(:create, true).as_json
|
14
|
+
end
|
15
|
+
|
16
|
+
def destroy
|
17
|
+
render json: create_presenter(:destroy).as_json
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
def create_presenter(type, once=false)
|
22
|
+
creation = case type
|
23
|
+
when :find
|
24
|
+
UserSettings::Key.find(@current_user.id, params[:key])
|
25
|
+
when :destroy
|
26
|
+
UserSettings::Key.destroy(@current_user.id, params[:key])
|
27
|
+
when :create
|
28
|
+
if once
|
29
|
+
UserSettings::Key.create(@current_user.id, params[:key], params[:value])
|
30
|
+
else
|
31
|
+
UserSettings::Key.create_or_update(@current_user.id, params[:key], params[:value])
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
UserSettingsPresenter.new creation
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module UserSettingsHelper
|
2
|
+
|
3
|
+
def javascript_include_tag_for_user_settings
|
4
|
+
javascript_include_tag 'user_settings'
|
5
|
+
end
|
6
|
+
|
7
|
+
def init_user_settings
|
8
|
+
js = <<-JAVSCRIPT.strip_heredoc
|
9
|
+
<script type="text/javascript">
|
10
|
+
$.userSettings = $.userSettings('#{UserSettings.base_path}');
|
11
|
+
</script>
|
12
|
+
JAVSCRIPT
|
13
|
+
js.html_safe
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module UserSettings
|
2
|
+
class Key
|
3
|
+
class << self
|
4
|
+
def create_or_update(user_id, key, value = nil)
|
5
|
+
UserSettings.redis.setex redisable_key(user_id, key), UserSettings.expiration_time, value
|
6
|
+
end
|
7
|
+
|
8
|
+
def create(user_id, key, value)
|
9
|
+
if find(user_id, key)
|
10
|
+
false
|
11
|
+
else
|
12
|
+
create_or_update(user_id, key, value)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def find(user_id, key)
|
17
|
+
UserSettings.redis.get redisable_key(user_id, key)
|
18
|
+
end
|
19
|
+
|
20
|
+
def destroy(user_id, key)
|
21
|
+
UserSettings.redis.del redisable_key(user_id, key)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
def redisable_key(user_id, key)
|
26
|
+
"usersetting:#{user_id}:#{key}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class UserSettingsPresenter
|
2
|
+
attr_reader :resource
|
3
|
+
|
4
|
+
def initialize(resource)
|
5
|
+
@resource = resource
|
6
|
+
end
|
7
|
+
|
8
|
+
def as_json
|
9
|
+
if @resource == false || @resource == true || @resource == nil
|
10
|
+
{ success: @resource || false }
|
11
|
+
else
|
12
|
+
{
|
13
|
+
success: true,
|
14
|
+
value: @resource
|
15
|
+
}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|