settr 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/.gitignore +4 -0
  2. data/Gemfile +3 -0
  3. data/README.md +29 -0
  4. data/Rakefile +1 -0
  5. data/app/assets/stylesheets/settr.css.scss +25 -0
  6. data/app/controllers/settr/application_controller.rb +3 -0
  7. data/app/controllers/settr/settings_controller.rb +77 -0
  8. data/app/helpers/application_helper.rb +2 -0
  9. data/app/helpers/settr/settings_helper.rb +14 -0
  10. data/app/models/.gitkeep +0 -0
  11. data/app/models/settr_setting.rb +24 -0
  12. data/app/views/layouts/application.html.erb +14 -0
  13. data/app/views/settr/settings/_form.html.erb +26 -0
  14. data/app/views/settr/settings/_setting.html.erb +39 -0
  15. data/app/views/settr/settings/_settings_form.html.erb +4 -0
  16. data/app/views/settr/settings/edit.html.erb +3 -0
  17. data/app/views/settr/settings/index.html.erb +27 -0
  18. data/app/views/settr/settings/new.html.erb +3 -0
  19. data/app/views/settr/settings/settings.html.erb +1 -0
  20. data/config/locales/de.yml +36 -0
  21. data/config/locales/en.yml +36 -0
  22. data/config/routes.rb +12 -0
  23. data/db/migrate/20110506184758_create_settr_settings.rb +16 -0
  24. data/lib/generators/settr/install_generator.rb +18 -0
  25. data/lib/generators/settr/templates/settr.rb +8 -0
  26. data/lib/settr.rb +84 -0
  27. data/lib/settr/engine.rb +4 -0
  28. data/lib/settr/version.rb +3 -0
  29. data/script/rails +6 -0
  30. data/settr.gemspec +23 -0
  31. data/test/dummy/.gitignore +15 -0
  32. data/test/dummy/Gemfile +8 -0
  33. data/test/dummy/Rakefile +7 -0
  34. data/test/dummy/app/assets/images/rails.png +0 -0
  35. data/test/dummy/app/assets/javascripts/application.js +9 -0
  36. data/test/dummy/app/assets/stylesheets/application.css +8 -0
  37. data/test/dummy/app/controllers/application_controller.rb +3 -0
  38. data/test/dummy/app/helpers/application_helper.rb +2 -0
  39. data/test/dummy/app/mailers/.gitkeep +0 -0
  40. data/test/dummy/app/models/.gitkeep +0 -0
  41. data/test/dummy/app/views/layouts/application.html.erb +20 -0
  42. data/test/dummy/app/views/settr/settings/settings.html.erb +5 -0
  43. data/test/dummy/config.ru +4 -0
  44. data/test/dummy/config/application.rb +50 -0
  45. data/test/dummy/config/boot.rb +8 -0
  46. data/test/dummy/config/database.yml +25 -0
  47. data/test/dummy/config/environment.rb +5 -0
  48. data/test/dummy/config/environments/development.rb +30 -0
  49. data/test/dummy/config/environments/production.rb +60 -0
  50. data/test/dummy/config/environments/test.rb +39 -0
  51. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  52. data/test/dummy/config/initializers/inflections.rb +10 -0
  53. data/test/dummy/config/initializers/mime_types.rb +5 -0
  54. data/test/dummy/config/initializers/secret_token.rb +7 -0
  55. data/test/dummy/config/initializers/session_store.rb +8 -0
  56. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  57. data/test/dummy/config/locales/de.yml +21 -0
  58. data/test/dummy/config/locales/en.yml +21 -0
  59. data/test/dummy/config/routes.rb +3 -0
  60. data/test/dummy/db/schema.rb +26 -0
  61. data/test/dummy/db/seeds.rb +35 -0
  62. data/test/dummy/doc/README_FOR_APP +2 -0
  63. data/test/dummy/lib/assets/.gitkeep +0 -0
  64. data/test/dummy/lib/tasks/.gitkeep +0 -0
  65. data/test/dummy/log/.gitkeep +0 -0
  66. data/test/dummy/public/404.html +26 -0
  67. data/test/dummy/public/422.html +26 -0
  68. data/test/dummy/public/500.html +26 -0
  69. data/test/dummy/public/favicon.ico +0 -0
  70. data/test/dummy/public/robots.txt +5 -0
  71. data/test/dummy/script/rails +6 -0
  72. data/test/dummy/vendor/assets/stylesheets/.gitkeep +0 -0
  73. data/test/dummy/vendor/plugins/.gitkeep +0 -0
  74. data/test/fixtures/.gitkeep +0 -0
  75. data/test/functional/.gitkeep +0 -0
  76. data/test/integration/.gitkeep +0 -0
  77. data/test/performance/browsing_test.rb +12 -0
  78. data/test/test_helper.rb +13 -0
  79. data/test/unit/.gitkeep +0 -0
  80. metadata +219 -0
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
@@ -0,0 +1,29 @@
1
+ Settr
2
+ =====
3
+
4
+ Settr is a hierarchical key-value based storage for configuration settings in Rails apps.
5
+
6
+ Usage
7
+ -----
8
+
9
+ Use
10
+
11
+ `rails g settr:install`
12
+
13
+ to install settr and modify */config/initializers/settr.rb* to your needs.
14
+
15
+ The path to settr settings is */settr/settings/settings*.
16
+
17
+ Check out the dummy app in the test directory (load seed data via `rake db:seed`).
18
+ One can access settr settings by e.g. `Settr.my_group.my_subgroup.my_setting`.
19
+
20
+ Authors
21
+ -------
22
+
23
+ * **Torsten Sprenger** – [ts@metaminded.com](mailto:ts@metaminded.com)
24
+ * **Peter Horn** – [ph@metaminded.com](mailto:ph@metaminded.com)
25
+
26
+ Copyright and License
27
+ ---------------------
28
+
29
+ Copyright 2012 metaminded UG. Licensed under the MIT License.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ .settr_form {
2
+ h1 {
3
+ font-size: 24px;
4
+ }
5
+
6
+ h2 {
7
+ font-size: 20px;
8
+ }
9
+
10
+ h3 {
11
+ font-size: 17px;
12
+ }
13
+
14
+ h4 {
15
+ font-size: 14px;
16
+ }
17
+
18
+ h5 {
19
+ font-size: 12px;
20
+ }
21
+
22
+ h6 {
23
+ font-size: 11px;
24
+ }
25
+ }
@@ -0,0 +1,3 @@
1
+ # encoding: utf-8
2
+ class Settr::ApplicationController < ApplicationController
3
+ end
@@ -0,0 +1,77 @@
1
+ # encoding: utf-8
2
+ class Settr::SettingsController < Settr::ApplicationController
3
+
4
+ def new
5
+ @setting = SettrSetting.new
6
+ end
7
+
8
+ def create
9
+ @setting = SettrSetting.new(params[:settr_setting])
10
+ if @setting.select?
11
+ unless @setting.options.include?(@setting.value)
12
+ flash[:error] = t('settr.setting.not_created_value_not_in_options')
13
+ redirect_to settr_settings_path
14
+ return
15
+ end
16
+ end
17
+
18
+ if @setting.save
19
+ flash[:success] = t('settr.setting.created')
20
+ else
21
+ flash[:error] = t('settr.setting.not_created')
22
+ end
23
+
24
+ redirect_to settr_settings_path
25
+ end
26
+
27
+ def edit
28
+ @setting = SettrSetting.find(params[:id])
29
+ end
30
+
31
+ def update
32
+ @setting = SettrSetting.find(params[:id])
33
+ @new_setting = SettrSetting.new(params[:settr_setting])
34
+ if @new_setting.select?
35
+ unless @new_setting.options.include?(@setting.value)
36
+ flash[:error] = t('settr.setting.not_updated_value_not_in_options')
37
+ redirect_to settr_settings_path
38
+ return
39
+ end
40
+ end
41
+
42
+ if @setting.update_attributes(params[:settr_setting])
43
+ flash[:success] = t('settr.setting.updated')
44
+ else
45
+ flash[:error] = t('settr.setting.not_updated')
46
+ end
47
+ redirect_to settr_settings_path
48
+ end
49
+
50
+ def index
51
+ @settings = SettrSetting.order(:key)
52
+ end
53
+
54
+ def destroy
55
+ @setting = SettrSetting.find(params[:id])
56
+ if @setting.destroy
57
+ flash[:success] = t('settr.setting.deleted')
58
+ else
59
+ flash[:error] = t('settr.setting.not_deleted')
60
+ end
61
+ redirect_to settr_settings_path
62
+ end
63
+
64
+ def settings
65
+ @settings = Settr.all
66
+
67
+ if request.post?
68
+ params[:settr].each do |k, v|
69
+ setting = SettrSetting.find_by_key(k)
70
+ setting.value = (setting.typ == 'boolean' ? (v == '1').to_s : v)
71
+ setting.save!
72
+ end
73
+ flash[:success] = t('settr.settings.saved')
74
+ redirect_to settings_settr_settings_path
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,14 @@
1
+ class Settr
2
+ module SettingsHelper
3
+ def header(content)
4
+ html_options = Settr.header_class ? {:class => Settr.header_class} : {}
5
+ content_tag(Settr.header_tag.to_sym, content, html_options)
6
+ end
7
+
8
+ def link(body, url, html_options = {})
9
+ options = Settr.link_class ? {:class => Settr.link_class} : {}
10
+ options.update(html_options)
11
+ link_to(body, url, options)
12
+ end
13
+ end
14
+ end
File without changes
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ class SettrSetting < ActiveRecord::Base
3
+
4
+ def val
5
+ case typ
6
+ when 'boolean'
7
+ value == 'true'
8
+ when 'float'
9
+ value.to_f
10
+ when 'integer'
11
+ value.to_i
12
+ else
13
+ value
14
+ end
15
+ end
16
+
17
+ def select?
18
+ typ == 'select'
19
+ end
20
+
21
+ def options=(opts)
22
+ write_attribute :options, opts.gsub(' ','').gsub(',', ', ')
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Settr</title>
5
+ <%= stylesheet_link_tag "application" %>
6
+ <%= javascript_include_tag "application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,26 @@
1
+ <%= simple_form_for setting, :html => {:class => ['settr_form', Settr.form_class].compact.join(' ')} do |f| %>
2
+ <%= f.input :key, :label => t('settr.model.key') %>
3
+ <%= f.input :value, :label => t('settr.model.value') %>
4
+ <%= f.input :typ, :label => t('settr.model.typ'), :collection => ['string', 'text', 'boolean', 'integer', 'float', 'select'], :include_blank => false %>
5
+ <%= f.input :options, :label => t('settr.model.options'), :label_html => {:class => 'settr_setting_options_label'} %>
6
+ <%= f.input :alterable, :label => t('settr.model.alterable') %>
7
+ <%= f.submit (params[:action] == 'edit' ? t('settr.form.submit.edit') : t('settr.form.submit.new')) %>
8
+ <% end %>
9
+
10
+ <script type="text/javascript" charset="utf-8">
11
+ $(function(){
12
+ $(".settr_setting_options_label").hide();
13
+
14
+ <% unless setting.select? %>
15
+ $("#settr_setting_options").hide();
16
+ <% end %>
17
+
18
+ $("#settr_setting_typ").change(function(){
19
+ if ($("#settr_setting_typ :selected").text() == 'select') {
20
+ $("#settr_setting_options").show();
21
+ } else {
22
+ $("#settr_setting_options").hide().val('');
23
+ }
24
+ });
25
+ });
26
+ </script>
@@ -0,0 +1,39 @@
1
+ <% if object.is_a? SettrSetting %>
2
+ <% if object.alterable %>
3
+ <div class="clearfix string">
4
+ <% case object.typ %>
5
+ <% when 'boolean' %>
6
+ <label class="clearfix"><%= t("settr.#{object.key}") %></label>
7
+ <input type="hidden" name="settr[<%= object.key %>]" value="0" />
8
+ <input type="checkbox" name="settr[<%= object.key %>]" value="1" <%= 'checked' if object.val %> />
9
+ <% when 'string' %>
10
+ <label class="clearfix"><%= t("settr.#{object.key}") %></label>
11
+ <input type="text" name="settr[<%= object.key %>]" value="<%= object.value %>" />
12
+ <% when 'text' %>
13
+ <label class="clearfix"><%= t("settr.#{object.key}") %></label>
14
+ <textarea cols="10" rows="10" name="settr[<%= object.key %>]"><%= object.value %></textarea>
15
+ <% when 'float' %>
16
+ <label class="clearfix"><%= t("settr.#{object.key}") %></label>
17
+ <input type="text" name="settr[<%= object.key %>]" value="<%= object.value %>" />
18
+ <% when 'integer' %>
19
+ <label class="clearfix"><%= t("settr.#{object.key}") %></label>
20
+ <input type="text" name="settr[<%= object.key %>]" value="<%= object.value %>" />
21
+ <% when 'select' %>
22
+ <label class="clearfix"><%= t("settr.#{object.key}._title") %></label>
23
+ <select name="settr[<%= object.key %>]">
24
+ <% object.options.split(', ').each do |option| %>
25
+ <option value="<%= option %>" <%= 'selected' if object.value == option %> ><%= t("settr.#{object.key}.#{option}") %></option>
26
+ <% end %>
27
+ </select>
28
+ <% end %>
29
+ </div>
30
+ <% end %>
31
+ <% else %>
32
+ <%= content_tag "h#{level}" do %>
33
+ <%= t("settr.#{key}._title") %>
34
+ <% end if level > 0 %>
35
+ <% object.sort_by{|k, v| k}.each do |k, v| %>
36
+ <%= render :partial => 'setting', :locals => {:object => v, :level => level + 1, :key => "#{key}.#{k}"} %>
37
+ <% end %>
38
+ <div class="horizontal_line grey"></div>
39
+ <% end %>
@@ -0,0 +1,4 @@
1
+ <%= form_tag({}, {:class => ['simple_form', 'settr_form', Settr.form_class].compact.join(' ')}) do %>
2
+ <%= render :partial => 'setting', :locals => {:object => @settings, :level => 0, :key => ''} %>
3
+ <%= submit_tag t('settr.form.submit.settings') %>
4
+ <% end %>
@@ -0,0 +1,3 @@
1
+ <%= header t('settr.title.edit') %>
2
+
3
+ <%= render :partial => 'form', :locals => {:setting => @setting} %>
@@ -0,0 +1,27 @@
1
+ <%= header t('settr.title.index') %>
2
+
3
+ <%= link t('settr.link.settings'), settings_settr_settings_path %>
4
+ <%= link t('settr.link.new'), new_settr_setting_path %>
5
+
6
+ <table class="<%= Settr.table_class %>">
7
+ <thead>
8
+ <tr>
9
+ <th><%= t('settr.model.key') %></th>
10
+ <th><%= t('settr.model.value') %></th>
11
+ <th><%= t('settr.model.typ') %></th>
12
+ <th><%= t('settr.model.options') %></th>
13
+ <th><%= t('settr.model.alterable') %></th>
14
+ </tr>
15
+ </thead>
16
+ <% @settings.each do |s| %>
17
+ <tr>
18
+ <td><%= s.key %></td>
19
+ <td><%= s.value %></td>
20
+ <td><%= s.typ %></td>
21
+ <td><%= s.options %></td>
22
+ <td><%= s.alterable %></td>
23
+ <td><%= link t('settr.link.destroy'), settr_setting_path(s), :method => :delete, :confirm => t('settr.link.really_destroy') %></td>
24
+ <td><%= link t('settr.link.edit'), edit_settr_setting_path(s) %></td>
25
+ </tr>
26
+ <% end %>
27
+ </table>
@@ -0,0 +1,3 @@
1
+ <%= header t('settr.title.new') %>
2
+
3
+ <%= render :partial => 'form', :locals => {:setting => @setting} %>
@@ -0,0 +1 @@
1
+ <%= render :partial => 'settings_form' %>
@@ -0,0 +1,36 @@
1
+ de:
2
+ settr:
3
+ model:
4
+ key: 'Schlüssel'
5
+ value: 'Wert'
6
+ typ: 'Datentyp'
7
+ alterable: 'Modifizierbar'
8
+ options: 'Auswahlmöglichkeiten'
9
+ form:
10
+ submit:
11
+ new: 'Einstellungsattribut anlegen'
12
+ edit: 'Einstellungsattribut aktualisieren'
13
+ settings: 'Einstellungen sichern'
14
+ title:
15
+ index: 'Einstellungsattribute'
16
+ new: 'Einstellungsattribut anlegen'
17
+ edit: 'Einstellungsattribut bearbeiten'
18
+ settings: 'Einstellungen'
19
+ link:
20
+ destroy: 'Löschen'
21
+ edit: 'Bearbeiten'
22
+ really_destroy: 'Wirklich löschen?'
23
+ new: 'Einstellungsattribut anlegen'
24
+ settings: 'Zu Einstellungen'
25
+ index: 'Alle Einstellungsattribute'
26
+ settings:
27
+ saved: 'Einstellungen gesichert'
28
+ setting:
29
+ created: 'Einstellungsattribut angelegt'
30
+ not_created: 'Einstellungsattribut konnte nicht angelegt werden'
31
+ not_created_value_not_in_options: 'Einstellungsattribut konnte nicht angelegt werden, da der angegebene Wert nicht bei den Auswahlmöglichkeiten auftritt'
32
+ updated: 'Einstellungsattribut aktualisiert'
33
+ not_updated: 'Einstellungsattribut konnte nicht aktualisiert werden'
34
+ not_updated_value_not_in_options: 'Einstellungsattribut konnte nicht aktualisiert werden, da der angegebene Wert nicht bei den Auswahlmöglichkeiten auftritt'
35
+ deleted: 'Einstellungsattribut gelöscht'
36
+ not_deleted: 'Einstellungsattribut konnte nicht gelöscht werden'
@@ -0,0 +1,36 @@
1
+ en:
2
+ settr:
3
+ model:
4
+ key: 'Key'
5
+ value: 'Value'
6
+ typ: 'Data Type'
7
+ alterable: 'Alterable'
8
+ options: 'Options'
9
+ form:
10
+ submit:
11
+ new: 'Create Setting'
12
+ edit: 'Update Setting'
13
+ settings: 'Save Settings'
14
+ title:
15
+ index: 'Settings'
16
+ new: 'Create Setting'
17
+ edit: 'Edit Setting'
18
+ settings: 'Settings'
19
+ link:
20
+ destroy: 'Delete'
21
+ edit: 'Edit'
22
+ really_destroy: 'Really delete?'
23
+ new: 'Create Setting'
24
+ settings: 'To Settings'
25
+ index: 'All Settings'
26
+ settings:
27
+ saved: 'Settings saved'
28
+ setting:
29
+ created: 'Setting created'
30
+ not_created: 'Setting could not be created'
31
+ not_created_value_not_in_options: 'Setting could not be created, because its value is not contained in options'
32
+ updated: 'Setting updated'
33
+ not_updated: 'Setting could not be updated'
34
+ not_updated_value_not_in_options: 'Setting could not be updated, because its value is not contained in options'
35
+ deleted: 'Setting deleted'
36
+ not_deleted: 'Setting could not be deleted'
@@ -0,0 +1,12 @@
1
+ Rails.application.routes.draw do
2
+ # settr
3
+ if Settr.resources
4
+ namespace :settr do
5
+ resources 'settings' do
6
+ match 'settings', :on => :collection
7
+ end
8
+ end
9
+ else
10
+ match 'settr/settings/settings' => 'settr/settings#settings'
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ class CreateSettrSettings < ActiveRecord::Migration
2
+ def up
3
+ create_table :settr_settings do |t|
4
+ t.string 'key', :unique => true
5
+ t.string 'value'
6
+ t.string 'typ'
7
+ t.string 'options', :default => ''
8
+ t.boolean 'alterable', :default => true
9
+ t.timestamps
10
+ end
11
+ end
12
+
13
+ def down
14
+ drop_table :settr_settings
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ class Settr
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc "install settr default files"
5
+
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ def copy_initializer
9
+ copy_file 'settr.rb', 'config/initializers/settr.rb'
10
+ end
11
+
12
+ def copy_and_run_migration
13
+ run 'rake settr_engine:install:migrations'
14
+ run 'rake db:migrate'
15
+ end
16
+ end
17
+ end
18
+ end