settings-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c4e9bce03cd856beb065a25036f6225d8b317cac
4
+ data.tar.gz: cc8028c3b03979931a15d6fe2e37f64d7f63c783
5
+ SHA512:
6
+ metadata.gz: 3ef8f7a71b335071449eed1449b036411a42895df4bef168b4d01919d421686f7478ec8e4d6e73472adcc92708352f8ec481fcbe44a6717ede2b570889bb629d
7
+ data.tar.gz: 30be5fe7ee2f3272ad0b75ea3c2bb7387fa6218e4d01b56ed379fa847dea4f37afe6121ec10f845cae58ee1a1fc297d59a60b3d6836a0dcfca2255f1646ed660
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 vala
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.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
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 = 'SettingsRails'
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("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
@@ -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
+ module SettingsRails
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module SettingsRails
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module SettingsRails
2
+ class FloatSetting < SimpleValueSetting
3
+ end
4
+ end
@@ -0,0 +1,7 @@
1
+ module SettingsRails
2
+ class IntegerSetting < SimpleValueSetting
3
+ def value
4
+ super.to_i
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ module SettingsRails
2
+ class Setting < ActiveRecord::Base
3
+ def self.for(type)
4
+ class_name = class_name_for(type)
5
+
6
+ begin
7
+ class_name.constantize
8
+ rescue NameError => e
9
+ e.message = "Setting type does not exist : #{ type } ...\n#{ e.message }"
10
+ raise e
11
+ end
12
+ end
13
+
14
+ def self.class_name_for(type)
15
+ model_name = [type.to_s.camelize, 'Setting'].join
16
+ ['SettingsRails', model_name].join('::')
17
+ end
18
+
19
+ def _type
20
+ (type || self.class.name).demodulize.underscore.gsub(/_setting$/, '')
21
+ end
22
+
23
+ def _type=(val)
24
+ self.type = self.class.class_name_for(val.to_s)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,9 @@
1
+ module SettingsRails
2
+ class SimpleValueSetting < Setting
3
+ store_accessor :data, :value
4
+
5
+ def assign(val)
6
+ self.value = val
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module SettingsRails
2
+ class StringSetting < SimpleValueSetting
3
+ end
4
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>SettingsRails</title>
5
+ <%= stylesheet_link_tag "settings_rails/application", media: "all" %>
6
+ <%= javascript_include_tag "settings_rails/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ SettingsRails::Engine.routes.draw do
2
+ end
@@ -0,0 +1,12 @@
1
+ class CreateSettingsRailsSettings < ActiveRecord::Migration
2
+ def change
3
+ create_table :settings_rails_settings do |t|
4
+ t.string :key
5
+ t.json :data
6
+
7
+ t.string :type
8
+
9
+ t.timestamps null: false
10
+ end
11
+ end
12
+ end
@@ -0,0 +1 @@
1
+ require 'settings_rails'
@@ -0,0 +1,8 @@
1
+ require "settings_rails/cache"
2
+ require "settings_rails/helper"
3
+ require "settings_rails/form"
4
+
5
+ require "settings_rails/engine"
6
+
7
+ module SettingsRails
8
+ end
@@ -0,0 +1,29 @@
1
+ module SettingsRails
2
+ class Cache
3
+ def self.fetch(key)
4
+ instance.fetch(key)
5
+ end
6
+
7
+ def self.add(key, setting)
8
+ instance.store[key] = setting
9
+ end
10
+
11
+ def self.instance
12
+ RequestStore.store['settings_rails.cache'] ||= new
13
+ end
14
+
15
+ def self.each(&block)
16
+ instance.store.each(&block)
17
+ end
18
+
19
+ def fetch(key)
20
+ store[key]
21
+ end
22
+
23
+ def store
24
+ @store ||= SettingsRails::Setting.all.each_with_object({}.with_indifferent_access) do |setting, hash|
25
+ hash[setting.key] = setting
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ module SettingsRails
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace SettingsRails
4
+ end
5
+ end
@@ -0,0 +1,34 @@
1
+ module SettingsRails
2
+ class Form
3
+ include ActiveModel::Model
4
+
5
+ def settings
6
+ dirty_settings.values
7
+ end
8
+
9
+ def dirty_settings
10
+ @dirty_settings ||= {}
11
+ end
12
+
13
+ def settings_attributes=(params)
14
+ params.each do |_, attributes|
15
+ setting = Settings.get(attributes[:key], attributes[:_type])
16
+ setting.assign_attributes(attributes)
17
+
18
+ dirty_settings[setting.key] = setting
19
+ end
20
+ end
21
+
22
+ def save
23
+ ActiveRecord::Base.transaction do
24
+ dirty_settings.each do |_, setting|
25
+ setting.save
26
+ end
27
+ end
28
+ end
29
+
30
+ def persisted?
31
+ true
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,13 @@
1
+ class Settings
2
+ class << self
3
+ def get(key, type = :string)
4
+ SettingsRails::Cache.fetch(key) || build(key, type)
5
+ end
6
+
7
+ def build(key, type, value = nil)
8
+ setting = SettingsRails::Setting.for(type).new(key: key, value: value)
9
+ SettingsRails::Cache.add(key, setting)
10
+ setting
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module SettingsRails
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :settings_rails do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: settings-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Valentin Ballestrino
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: request_store
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Rails settings models with multiple data types, allowing db-driven global
56
+ configuration. PostgreSQL Only.
57
+ email:
58
+ - vala@glyph.fr
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - MIT-LICENSE
64
+ - Rakefile
65
+ - app/assets/javascripts/settings_rails/application.js
66
+ - app/assets/stylesheets/settings_rails/application.css
67
+ - app/controllers/settings_rails/application_controller.rb
68
+ - app/helpers/settings_rails/application_helper.rb
69
+ - app/models/settings_rails/float_setting.rb
70
+ - app/models/settings_rails/integer_setting.rb
71
+ - app/models/settings_rails/setting.rb
72
+ - app/models/settings_rails/simple_value_setting.rb
73
+ - app/models/settings_rails/string_setting.rb
74
+ - app/views/layouts/settings_rails/application.html.erb
75
+ - config/routes.rb
76
+ - db/migrate/20150715134533_create_settings_rails_settings.rb
77
+ - lib/settings-rails.rb
78
+ - lib/settings_rails.rb
79
+ - lib/settings_rails/cache.rb
80
+ - lib/settings_rails/engine.rb
81
+ - lib/settings_rails/form.rb
82
+ - lib/settings_rails/helper.rb
83
+ - lib/settings_rails/version.rb
84
+ - lib/tasks/settings_rails_tasks.rake
85
+ homepage: https://github.com/glyph-fr/settings-rails
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.4.4
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Rails settings models with multiple data types, allowing db-driven global
109
+ configuration. PostgreSQL Only.
110
+ test_files: []