rails-settings-ui 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem "sqlite3"
7
+ end
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 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.
data/README.md ADDED
@@ -0,0 +1,125 @@
1
+ Rails settings UI
2
+ ================================
3
+
4
+ A Rails Engine to manage your application settings. Includes validation. Compatible with Rails 3.
5
+ It compatible with [rails-settings-cached](https://github.com/huacnlee/rails-settings-cached) gem. Untested, but should work with rails-settings gem.
6
+
7
+ Preview:
8
+
9
+ ![ScreenShot](https://raw.github.com/accessd/rails-settings-ui/master/doc/img/settings-page.png)
10
+
11
+ How to
12
+ -----
13
+
14
+ Add to Gemfile
15
+
16
+ gem 'rails-settings-ui'
17
+ gem 'rails-settings-cached'
18
+ # or
19
+ gem 'rails-settings'
20
+ # or your fork of rails-settings
21
+
22
+ Setup:
23
+
24
+ # adds initializer and route:
25
+ rails g rails_settings_ui:install
26
+
27
+ Config
28
+ ------------
29
+
30
+ In config/initializers/rails_settings_ui.rb
31
+
32
+ RailsSettingsUi.setup do |config|
33
+ config.ignored_settings = [:company_name] # Settings not displayed in the interface
34
+ end
35
+
36
+ Routing
37
+ -------
38
+
39
+ # engine root:
40
+ rails_settings_ui_url
41
+
42
+ I18n
43
+ -------------
44
+
45
+ You can localize:
46
+
47
+ * Settings names, eg:
48
+
49
+ ```yaml
50
+ settings:
51
+ attributes:
52
+ launch_mode: # setting name
53
+ name: 'Launch mode'
54
+ ```
55
+
56
+ * Checkbox or select options labels for array options, eg:
57
+
58
+ ```yaml
59
+ settings:
60
+ attributes:
61
+ launch_mode:
62
+ labels:
63
+ auto: 'Auto mode'
64
+ manual: 'Manual mode'
65
+ ```
66
+
67
+ * Help blocks for settings, eg:
68
+
69
+ ```yaml
70
+ settings:
71
+ attributes:
72
+ launch_mode:
73
+ help_block: 'Rocket launch mode'
74
+ ```
75
+
76
+ Validations
77
+ -------------
78
+
79
+ To validation work is required the default settings in the proper format, eg:
80
+
81
+ class Settings < RailsSettings::CachedSettings
82
+ defaults[:company_name] = "Company name"
83
+ defaults[:head_name] = "Head name"
84
+ defaults[:manager_premium] = 19
85
+ defaults[:show_contract_fields] = true
86
+ defaults[:launch_mode] = [:auto, :manual]
87
+ end
88
+
89
+ Views
90
+ -------------
91
+ Rails.application.config.to_prepare do
92
+ # Use admin layout:
93
+ RailsSettingsUi::ApplicationController.module_eval do
94
+ layout 'admin'
95
+ end
96
+ # If you are using a custom layout, you will want to make app routes available to rails-setting-ui:
97
+ RailsSettingsUi.inline_main_app_routes!
98
+ end
99
+
100
+
101
+ Authentication & authorization
102
+ ------------------------------
103
+
104
+ You can specify the parent controller for settings controller, and it will inherit all before filters.
105
+ Note that this must be placed before any other references to rails-setting-ui application controller in the initializer:
106
+
107
+ RailsSettingsUi.parent_controller = 'Admin::ApplicationController' # default: '::ApplicationController'
108
+
109
+ Alternatively, to have custom rules just for rails-setting-ui you can:
110
+
111
+ Rails.application.config.to_prepare do
112
+ RailsSettingsUi::ApplicationController.module_eval do
113
+ before_filter :check_settings_permissions
114
+
115
+ private
116
+ def check_settings_permissions
117
+ render status: 403 unless current_user && can_manage_settings?(current_user)
118
+ end
119
+ end
120
+ end
121
+
122
+
123
+
124
+
125
+ This project uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rake'
10
+ require 'rake/rdoctask'
11
+
12
+ require 'rake/testtask'
13
+
14
+ Rake::TestTask.new(:test) do |t|
15
+ t.libs << 'lib'
16
+ t.libs << 'test'
17
+ t.pattern = 'test/**/*_test.rb'
18
+ t.verbose = false
19
+ end
20
+
21
+ task :default => :test
@@ -0,0 +1,4 @@
1
+ /*
2
+ *= require bootstrap_and_overrides
3
+ *= require_self
4
+ */
@@ -0,0 +1,27 @@
1
+ @import "twitter/bootstrap/bootstrap";
2
+ body { padding-top: 60px; }
3
+
4
+ @import "twitter/bootstrap/responsive";
5
+
6
+ // Set the correct sprite paths
7
+ @iconSpritePath: asset-path('twitter/bootstrap/glyphicons-halflings.png');
8
+ @iconWhiteSpritePath: asset-path('twitter/bootstrap/glyphicons-halflings-white.png');
9
+
10
+ // Font Awesome
11
+ @fontAwesomeEotPath: asset-url("fontawesome-webfont.eot");
12
+ @fontAwesomeEotPath_iefix: asset-url("fontawesome-webfont.eot?#iefix");
13
+ @fontAwesomeWoffPath: asset-url("fontawesome-webfont.woff");
14
+ @fontAwesomeTtfPath: asset-url("fontawesome-webfont.ttf");
15
+ @fontAwesomeSvgPath: asset-url("fontawesome-webfont.svg#fontawesomeregular");
16
+ @import "fontawesome/font-awesome";
17
+
18
+ // Your custom LESS stylesheets goes here
19
+ //
20
+ // Since bootstrap was imported above you have access to its mixins which
21
+ // you may use and inherit here
22
+ //
23
+ // If you'd like to override bootstrap's own variables, you can do so here as well
24
+ // See http://twitter.github.com/bootstrap/less.html for their names and documentation
25
+ //
26
+ // Example:
27
+ // @linkColor: #ff0000;
@@ -0,0 +1,4 @@
1
+ module RailsSettingsUi
2
+ class ApplicationController < ::RailsSettingsUi.parent_controller.constantize
3
+ end
4
+ end
@@ -0,0 +1,29 @@
1
+ class RailsSettingsUi::SettingsController < RailsSettingsUi::ApplicationController
2
+ before_filter :collection
3
+ before_filter :cast_settings_params, only: :update_all
4
+
5
+ def index
6
+ end
7
+
8
+ def update_all
9
+ if @casted_settings[:errors].any?
10
+ render :index
11
+ else
12
+ @casted_settings.map { |setting| Settings[setting[0]] = setting[1] if setting[0] != "errors" }
13
+ redirect_to [:settings]
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def collection
20
+ I18n.locale = :en
21
+ all_settings = Settings.defaults.merge(Settings.all)
22
+ all_settings_without_ignored = all_settings.reject{ |name, description| RailsSettingsUi.ignored_settings.include?(name.to_sym) }
23
+ @settings = Hash[all_settings_without_ignored]
24
+ end
25
+
26
+ def cast_settings_params
27
+ @casted_settings = RailsSettingsUi::SettingsHelper.cast(params["settings"])
28
+ end
29
+ end
@@ -0,0 +1,75 @@
1
+ module RailsSettingsUi::SettingsHelper
2
+ def self.cast(requires_cast)
3
+ requires_cast[:errors] = {}
4
+ requires_cast.each do |var_name, value|
5
+ case Settings.defaults[var_name.to_sym]
6
+ when Fixnum
7
+ if is_numeric?(value)
8
+ requires_cast[var_name] = value.to_i
9
+ else
10
+ requires_cast[:errors][var_name.to_sym] = I18n.t("settings.errors.invalid_numeric", default: 'Invalid')
11
+ end
12
+ when ActiveSupport::HashWithIndifferentAccess
13
+ begin
14
+ requires_cast[var_name] = JSON.parse(value.gsub(/\=\>/, ':'))
15
+ rescue JSON::ParserError => e
16
+ requires_cast[:errors][var_name.to_sym] = I18n.t("settings.errors.invalid_hash", default: 'Invalid')
17
+ end
18
+ when Float
19
+ if is_numeric?(value)
20
+ requires_cast[var_name] = value.to_f
21
+ else
22
+ requires_cast[:errors][var_name.to_sym] = I18n.t("settings.errors.invalid_numeric", default: 'Invalid')
23
+ end
24
+ when Array
25
+ # Array presented in checkboxes
26
+ if value.is_a?(Hash)
27
+ requires_cast[var_name] = value.keys.map!(&:to_sym)
28
+ # or in select tag
29
+ else
30
+ value.to_sym
31
+ end
32
+ when FalseClass, TrueClass
33
+ requires_cast[var_name] = true
34
+ end
35
+ end
36
+ Settings.defaults.each do |name, value|
37
+ if !requires_cast[name.to_sym].present? && [TrueClass, FalseClass].include?(value.class)
38
+ requires_cast[name.to_sym] = false
39
+ end
40
+ end
41
+
42
+ requires_cast
43
+ end
44
+
45
+ def setting_field(setting_name, setting_value)
46
+ if RailsSettingsUi.settings_displayed_as_select_tag.include?(setting_name.to_sym)
47
+ default_setting_values = Settings.defaults[setting_name.to_s].map do |setting_value|
48
+ [I18n.t("settings.attributes.#{setting_name}.labels.#{setting_value}", default: setting_value.to_s), setting_value]
49
+ end
50
+ select_tag("settings[#{setting_name.to_s}]", options_for_select(default_setting_values, setting_value))
51
+ elsif setting_value.is_a?(Array)
52
+ field = ""
53
+ Settings.defaults[setting_name.to_sym].each do |value|
54
+ field << check_box_tag("settings[#{setting_name.to_s}][#{value.to_s}]", nil, Settings.defaults.merge(Settings.all)[setting_name.to_s].include?(value), style: "margin: 0 10px;")
55
+ field << label_tag("settings[#{setting_name.to_s}][#{value.to_s}]", I18n.t("settings.attributes.#{setting_name}.labels.#{value}", default: value.to_s), style: "display: inline-block;")
56
+ end
57
+ return field.html_safe
58
+ elsif [TrueClass, FalseClass].include?(setting_value.class)
59
+ return check_box_tag("settings[#{setting_name.to_s}]", nil, setting_value).html_safe
60
+ else
61
+ text_field = if setting_value.to_s.size > 30
62
+ text_area_tag("settings[#{setting_name}]", setting_value.to_s, rows: 10)
63
+ else
64
+ text_field_tag("settings[#{setting_name}]", setting_value.to_s)
65
+ end
66
+
67
+ help_block_content = I18n.t("settings.attributes.#{setting_name}.help_block", default: '')
68
+ text_field + (help_block_content.presence && content_tag(:span, help_block_content, class: 'help-block'))
69
+ end
70
+ end
71
+
72
+ def self.is_numeric?(value)
73
+ !value.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil?
74
+ end
75
+ end
@@ -0,0 +1,18 @@
1
+ !!!
2
+ %html
3
+ %head
4
+ %title Rails settings
5
+ %meta{:charset => "utf-8"}
6
+ %meta{"http-equiv" => "X-UA-Compatible", :content => "IE=edge,chrome=1"}
7
+ %meta{:name => "viewport", :content => "width=device-width, initial-scale=1, maximum-scale=1"}
8
+ = stylesheet_link_tag :application
9
+ = csrf_meta_tags
10
+ %body
11
+ .navbar.navbar-fixed-top
12
+ .navbar-inner
13
+ .container-fluid
14
+ %a.brand{href: "/"} Rails settings ui
15
+ .container-fluid
16
+ .row-fluid
17
+ .content= yield
18
+ %footer
@@ -0,0 +1,25 @@
1
+ %ul.breadcrumb
2
+ %li.active= t('settings.index.title')
3
+
4
+ - if @casted_settings && @casted_settings[:errors].any?
5
+ .alert.alert-error= t('settings.index.settings_include_errors')
6
+
7
+ = form_tag [:update_all], method: :put, class: 'form-inline' do
8
+ %table.table.table-striped
9
+ %thead
10
+ %tr
11
+ %th= t('settings.index.description')
12
+ %th= t('settings.index.value')
13
+ %tbody
14
+ - @settings.each do |name, value|
15
+ - if name != :errors
16
+ %tr
17
+ %td= I18n.t("settings.attributes.#{name}.name")
18
+ %td
19
+ - setting_with_error = @casted_settings && @casted_settings[:errors].include?(name)
20
+ %div{class: "control-group #{ setting_with_error ? 'error' : ''}"}
21
+ = setting_field(name, value)
22
+ - if setting_with_error
23
+ %span.help-inline= @casted_settings[:errors][name]
24
+
25
+ = submit_tag t('settings.index.save_all'), class: 'btn btn-info'
@@ -0,0 +1,14 @@
1
+ require 'rails-settings-ui'
2
+
3
+ #= Application-specific
4
+ #
5
+ # # You can specify a controller for RailsSettingsUi::ApplicationController to inherit from:
6
+ # RailsSettingsUi.parent_controller = 'Admin::ApplicationController' # default: '::ApplicationController'
7
+ #
8
+ # # Render RailsSettingsUi inside a custom layout (set to 'application' to use app layout, default is RailsSettingsUi's own layout)
9
+ # RailsSettingsUi::ApplicationController.layout 'admin'
10
+
11
+ Rails.application.config.to_prepare do
12
+ # If you use a *custom layout*, make route helpers available to RailsSettingsUi:
13
+ # RailsSettingsUi.inline_main_app_routes!
14
+ end
@@ -0,0 +1,11 @@
1
+ en:
2
+ settings:
3
+ index:
4
+ title: Settings
5
+ description: Description
6
+ value: Value
7
+ save_all: Save all
8
+ settings_include_errors: Settings include errors
9
+ errors:
10
+ invalid_numeric: Invalid numeric
11
+ invalid_hash: Invalid hash
@@ -0,0 +1,11 @@
1
+ ru:
2
+ settings:
3
+ index:
4
+ title: Настройки
5
+ description: Описание
6
+ value: Значение
7
+ save_all: Сохранить
8
+ settings_include_errors: Ошибка в настройках
9
+ errors:
10
+ invalid_numeric: Невалидный формат числа
11
+ invalid_hash: Невалидный хэш
data/config/routes.rb ADDED
@@ -0,0 +1,4 @@
1
+ RailsSettingsUi::Engine.routes.draw do
2
+ get '/' => 'settings#index', as: :settings
3
+ put '/update_all' => 'settings#update_all'
4
+ end
@@ -0,0 +1,17 @@
1
+ module RailsSettingsUi
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc "creates an initializer file at config/initializers/rails_settings_ui.rb and adds route to config/routes.rb"
5
+ source_root File.expand_path('../../../..', __FILE__)
6
+
7
+ def generate_initialization
8
+ copy_file 'config/initializers/rails_settings_ui.rb', 'config/initializers/rails_settings_ui.rb'
9
+ end
10
+
11
+ def generate_routing
12
+ route "mount RailsSettingsUi::Engine, at: 'settings'"
13
+ log "# You can access RailsSettingsUi urls like this: rails_settings_ui_path #=> '/settings'"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ require 'rails-settings-ui/engine'
3
+ require 'rails-settings-ui/main_app_route_delegator'
4
+ require 'rails-settings-ui/version'
5
+
6
+ require 'rails-settings-cached'
7
+
8
+ module RailsSettingsUi
9
+ mattr_accessor :parent_controller
10
+ self.parent_controller = '::ApplicationController'
11
+
12
+ # Settings not displayed in the interface (eg. [:launch_mode, :project_name])
13
+ mattr_accessor :ignored_settings
14
+ self.ignored_settings = []
15
+
16
+ # Settings displayed in the interface as select tag instead checkboxes (useful for array with one possible choice)
17
+ mattr_accessor :settings_displayed_as_select_tag
18
+ self.settings_displayed_as_select_tag = []
19
+
20
+ class << self
21
+ def inline_main_app_routes!
22
+ ::RailsSettingsUi::ApplicationController.helper ::RailsSettingsUi::MainAppRouteDelegator
23
+ end
24
+
25
+ def setup
26
+ yield self
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ module ::RailsSettingsUi
2
+ class Engine < Rails::Engine
3
+ isolate_namespace RailsSettingsUi
4
+ end
5
+ end
@@ -0,0 +1,19 @@
1
+ module RailsSettingsUi::MainAppRouteDelegator
2
+ # delegate url helpers to main_app
3
+ def method_missing(method, *args, &block)
4
+ if main_app_route_method?(method)
5
+ main_app.send(method, *args)
6
+ else
7
+ super
8
+ end
9
+ end
10
+
11
+ def respond_to?(method)
12
+ super || main_app_route_method?(method)
13
+ end
14
+
15
+ private
16
+ def main_app_route_method?(method)
17
+ method.to_s =~ /_(?:path|url)$/ && main_app.respond_to?(method)
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module RailsSettingsUi
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-settings-ui
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrey Morskov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.1'
30
+ - !ruby/object:Gem::Dependency
31
+ name: i18n
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: haml-rails
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: twitter-bootstrap-rails
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: User interface for manage settings with rails-settings gem
79
+ email: accessd0@gmail.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files: []
83
+ files:
84
+ - app/assets/stylesheets/rails_settings_ui/application.css.scss
85
+ - app/assets/stylesheets/rails_settings_ui/bootstrap_and_overrides.css.less
86
+ - app/controllers/rails_settings_ui/application_controller.rb
87
+ - app/controllers/rails_settings_ui/settings_controller.rb
88
+ - app/helpers/rails_settings_ui/settings_helper.rb
89
+ - app/views/layouts/rails_settings_ui/application.html.haml
90
+ - app/views/rails_settings_ui/settings/index.html.haml
91
+ - lib/generators/rails_settings_ui/install_generator.rb
92
+ - lib/rails-settings-ui/engine.rb
93
+ - lib/rails-settings-ui/main_app_route_delegator.rb
94
+ - lib/rails-settings-ui/version.rb
95
+ - lib/rails-settings-ui.rb
96
+ - config/initializers/rails_settings_ui.rb
97
+ - config/locales/en.yml
98
+ - config/locales/ru.yml
99
+ - config/routes.rb
100
+ - MIT-LICENSE
101
+ - Rakefile
102
+ - Gemfile
103
+ - README.md
104
+ homepage: https://github.com/accessd/rails-settings-ui
105
+ licenses:
106
+ - MIT
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 1.8.23
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: User interface for manage settings (rails engine)
129
+ test_files: []