rails-settings-ui 0.5.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -1
  3. data/app/controllers/rails_settings_ui/settings_controller.rb +15 -6
  4. data/app/helpers/rails_settings_ui/settings_helper.rb +1 -1
  5. data/app/views/layouts/rails_settings_ui/application.html.erb +26 -0
  6. data/app/views/rails_settings_ui/settings/index.html.erb +45 -0
  7. data/config/locales/en.yml +4 -2
  8. data/config/locales/ru.yml +4 -0
  9. data/config/locales/zh-CN.yml +4 -2
  10. data/config/locales/zh-TW.yml +4 -2
  11. data/lib/rails-settings-ui.rb +2 -1
  12. data/lib/rails-settings-ui/engine.rb +0 -2
  13. data/lib/rails-settings-ui/settings_form_coercible.rb +91 -0
  14. data/lib/rails-settings-ui/settings_form_validator.rb +62 -0
  15. data/lib/rails-settings-ui/version.rb +1 -1
  16. data/spec/dummy/app/models/settings.rb +3 -0
  17. data/spec/dummy/config/application.rb +1 -1
  18. data/spec/dummy/config/environments/production.rb +1 -1
  19. data/spec/dummy/config/environments/test.rb +3 -1
  20. data/spec/dummy/config/locales/en.yml +6 -1
  21. data/spec/dummy/log/test.log +27323 -0
  22. data/spec/dummy/tmp/cache/5B0/540/settings%3Amode +1 -0
  23. data/spec/dummy/tmp/cache/612/400/settings%3Aangle +1 -0
  24. data/spec/dummy/tmp/cache/62A/7E0/settings%3Alimit +1 -0
  25. data/spec/dummy/tmp/cache/62C/9C0/settings%3Atimer +1 -0
  26. data/spec/dummy/tmp/cache/63C/E80/settings%3Astyle +3 -0
  27. data/spec/dummy/tmp/cache/702/EF0/settings%3Acompany +1 -1
  28. data/spec/dummy/tmp/cache/8AF/EF0/settings%3Adescription +1 -0
  29. data/spec/dummy/tmp/cache/902/3E0/settings%3Aproject_name +2 -0
  30. data/spec/dummy/tmp/cache/A05/190/settings%3Aproject_status +1 -0
  31. data/spec/dummy/tmp/cache/A36/6E0/settings%3Acheck_something +1 -0
  32. data/spec/dummy/tmp/cache/AE1/750/rails_settings_cached%3Amode +1 -0
  33. data/spec/dummy/tmp/cache/B43/920/rails_settings_cached%3Aangle +1 -0
  34. data/spec/dummy/tmp/cache/B5B/D00/rails_settings_cached%3Alimit +1 -0
  35. data/spec/dummy/tmp/cache/B5D/EE0/rails_settings_cached%3Atimer +1 -0
  36. data/spec/dummy/tmp/cache/B6D/3A0/rails_settings_cached%3Astyle +3 -0
  37. data/spec/dummy/tmp/cache/DE0/670/rails_settings_cached%3Adescription +1 -0
  38. data/spec/dummy/tmp/cache/E33/F60/rails_settings_cached%3Aproject_name +2 -0
  39. data/spec/dummy/tmp/cache/F36/330/rails_settings_cached%3Aproject_status +1 -0
  40. data/spec/dummy/tmp/cache/F67/B90/rails_settings_cached%3Acheck_something +1 -0
  41. data/spec/dummy/tmp/capybara/capybara-201604072218331731576536.html +110 -0
  42. data/spec/dummy/tmp/capybara/capybara-201604081032444980849427.html +110 -0
  43. data/spec/dummy/tmp/capybara/capybara-201604081033515090839056.html +110 -0
  44. data/spec/dummy/tmp/capybara/capybara-201604081035235018036538.html +110 -0
  45. data/spec/dummy/tmp/capybara/capybara-201604081035456240700789.html +110 -0
  46. data/spec/dummy/tmp/capybara/capybara-201604171939409580658412.html +167 -0
  47. data/spec/dummy/tmp/capybara/capybara-201604172001578955442533.html +167 -0
  48. data/spec/dummy/tmp/capybara/capybara-201604172002367777806457.html +167 -0
  49. data/spec/dummy/tmp/capybara/capybara-201604172004318143886680.html +167 -0
  50. data/spec/dummy/tmp/capybara/capybara-201604172005177937139040.html +167 -0
  51. data/spec/dummy/tmp/capybara/capybara-201604172053441599257667.html +167 -0
  52. data/spec/dummy/tmp/capybara/capybara-20160417205725250944476.html +167 -0
  53. data/spec/dummy/tmp/capybara/capybara-201604172057331132801259.html +167 -0
  54. data/spec/dummy/tmp/capybara/capybara-201604172118071632087268.html +167 -0
  55. data/spec/dummy/tmp/capybara/capybara-201604172127076718204966.html +168 -0
  56. data/spec/dummy/tmp/capybara/capybara-20160417213155185601243.html +168 -0
  57. data/spec/dummy/tmp/capybara/capybara-201604172132268322962171.html +168 -0
  58. data/spec/dummy/tmp/capybara/capybara-201604172134068049534551.html +168 -0
  59. data/spec/dummy/tmp/capybara/capybara-201604172134532268725182.html +168 -0
  60. data/spec/dummy/tmp/capybara/capybara-20160417213510406143193.html +168 -0
  61. data/spec/dummy/tmp/capybara/capybara-201604172137188286690681.html +168 -0
  62. data/spec/dummy/tmp/capybara/capybara-20160417214102473521215.html +167 -0
  63. data/spec/dummy/tmp/capybara/capybara-201604172142356828149817.html +167 -0
  64. data/spec/dummy/tmp/capybara/capybara-201604172224448946017859.html +168 -0
  65. data/spec/dummy/tmp/capybara/capybara-201604180843098131092841.html +168 -0
  66. data/spec/dummy/tmp/capybara/capybara-201604180904356085374829.html +167 -0
  67. data/spec/dummy/tmp/capybara/capybara-201604181000253189634139.html +167 -0
  68. data/spec/dummy/tmp/capybara/capybara-201604182056589084036047.html +167 -0
  69. data/spec/features/settings_spec.rb +103 -24
  70. data/spec/lib/rails-settings-ui/settings_form_coercible_spec.rb +139 -0
  71. data/spec/lib/rails-settings-ui/settings_form_validator_spec.rb +96 -0
  72. data/spec/spec_helper.rb +1 -0
  73. metadata +146 -36
  74. data/app/views/layouts/rails_settings_ui/application.html.haml +0 -16
  75. data/app/views/rails_settings_ui/settings/index.html.haml +0 -25
  76. data/lib/rails-settings-ui/type_converter.rb +0 -60
  77. data/lib/rails-settings-ui/value_types/array.rb +0 -19
  78. data/lib/rails-settings-ui/value_types/base.rb +0 -30
  79. data/lib/rails-settings-ui/value_types/boolean.rb +0 -16
  80. data/lib/rails-settings-ui/value_types/duration.rb +0 -17
  81. data/lib/rails-settings-ui/value_types/fixnum.rb +0 -17
  82. data/lib/rails-settings-ui/value_types/float.rb +0 -17
  83. data/lib/rails-settings-ui/value_types/hash.rb +0 -19
  84. data/lib/rails-settings-ui/value_types/string.rb +0 -11
  85. data/lib/rails-settings-ui/value_types/symbol.rb +0 -11
  86. data/spec/lib/rails-settings-ui/type_converter_spec.rb +0 -33
  87. data/spec/lib/rails-settings-ui/value_types/array_spec.rb +0 -21
  88. data/spec/lib/rails-settings-ui/value_types/base_spec.rb +0 -23
  89. data/spec/lib/rails-settings-ui/value_types/boolean_spec.rb +0 -10
  90. data/spec/lib/rails-settings-ui/value_types/duration_spec.rb +0 -19
  91. data/spec/lib/rails-settings-ui/value_types/fixnum_spec.rb +0 -17
  92. data/spec/lib/rails-settings-ui/value_types/float_spec.rb +0 -17
  93. data/spec/lib/rails-settings-ui/value_types/hash_spec.rb +0 -26
  94. data/spec/lib/rails-settings-ui/value_types/string_spec.rb +0 -10
  95. data/spec/lib/rails-settings-ui/value_types/symbol_spec.rb +0 -10
@@ -1,16 +0,0 @@
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
- = csrf_meta_tags
9
- %body
10
- .container
11
- .navbar.navbar-default
12
- .container-fluid
13
- .navbar-header
14
- %a.navbar-brand{href: "/"} Rails settings ui
15
- .row= yield
16
- %footer
@@ -1,25 +0,0 @@
1
- %ol.breadcrumb
2
- %li.active= t('settings.index.title')
3
-
4
- - if @casted_settings && @casted_settings[:errors].any?
5
- .alert.alert-danger= t('settings.index.settings_include_errors')
6
-
7
- = form_tag [:update_all], method: :put, class: 'form' 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{data: {name: name}}
17
- %td= I18n.t("settings.attributes.#{name}.name")
18
- %td.setting-value
19
- - setting_with_error = @casted_settings && @casted_settings[:errors].include?(name)
20
- %div{class: "form-group #{ setting_with_error ? 'has-error' : ''}"}
21
- = setting_field(name, value, @settings)
22
- - if setting_with_error
23
- %span.help-block= @casted_settings[:errors][name]
24
-
25
- = submit_tag t('settings.index.save_all'), class: 'btn btn-primary btn-lg'
@@ -1,60 +0,0 @@
1
- require_relative "value_types/string"
2
- require_relative "value_types/symbol"
3
- require_relative "value_types/fixnum"
4
- require_relative "value_types/hash"
5
- require_relative "value_types/duration"
6
- require_relative "value_types/float"
7
- require_relative "value_types/array"
8
- require_relative "value_types/boolean"
9
-
10
- module RailsSettingsUi
11
- class UnknownDefaultValueType < StandardError;end
12
-
13
- class TypeConverter
14
- VALUE_TYPES_MAP = {
15
- String => RailsSettingsUi::ValueTypes::String,
16
- Symbol => RailsSettingsUi::ValueTypes::Symbol,
17
- Fixnum => RailsSettingsUi::ValueTypes::Fixnum,
18
- ActiveSupport::HashWithIndifferentAccess => RailsSettingsUi::ValueTypes::Hash,
19
- ActiveSupport::Duration => RailsSettingsUi::ValueTypes::Float,
20
- Float => RailsSettingsUi::ValueTypes::Float,
21
- Array => RailsSettingsUi::ValueTypes::Array,
22
- FalseClass => RailsSettingsUi::ValueTypes::Boolean,
23
- TrueClass => RailsSettingsUi::ValueTypes::Boolean
24
- }
25
-
26
- def self.cast(settings)
27
- errors = {}
28
- settings.each do |name, value|
29
- type = setting_value_type(name, value)
30
- settings[name] = type.cast
31
- if type.errors.any?
32
- errors[name.to_sym] = type.errors.join(', ')
33
- end
34
- end
35
- settings = set_non_presented_boolean_settings_to_false(settings)
36
-
37
- settings[:errors] = errors
38
- settings
39
- end
40
-
41
- def self.setting_value_type(name, value)
42
- default_setting_value_type = RailsSettingsUi.settings_klass.defaults[name.to_sym].class
43
- unless VALUE_TYPES_MAP.keys.include?(default_setting_value_type)
44
- raise RailsSettingsUi::UnknownDefaultValueType, "Unknown default value type #{default_setting_value_type}"
45
- end
46
-
47
- setting_value_type_class = VALUE_TYPES_MAP[default_setting_value_type]
48
- setting_value_type_class.new(value)
49
- end
50
-
51
- def self.set_non_presented_boolean_settings_to_false(settings)
52
- RailsSettingsUi.settings_klass.defaults.each do |name, value|
53
- if !settings[name.to_sym].present? && [TrueClass, FalseClass].include?(value.class)
54
- settings[name.to_sym] = false
55
- end
56
- end
57
- settings
58
- end
59
- end
60
- end
@@ -1,19 +0,0 @@
1
- require_relative "base"
2
-
3
- module RailsSettingsUi
4
- module ValueTypes
5
- class Array < RailsSettingsUi::ValueTypes::Base
6
- def cast
7
- # Array presented in checkboxes
8
- if ["Hash", "ActiveSupport::HashWithIndifferentAccess"].include?(value.class.name)
9
- value.keys.map!(&:to_sym)
10
- elsif value.class.name == 'ActionController::Parameters'
11
- value.reject{|_,v| v!='on' }.keys.map!(&:to_sym)
12
- # or in select tag
13
- else
14
- value.to_sym
15
- end
16
- end
17
- end
18
- end
19
- end
@@ -1,30 +0,0 @@
1
- module RailsSettingsUi
2
- module ValueTypes
3
- class Base
4
- attr_accessor :value, :errors
5
-
6
- def initialize(value)
7
- self.value = value
8
- self.errors = []
9
- validate
10
- end
11
-
12
- def cast
13
- raise NotImplementedError
14
- end
15
-
16
- def valid?
17
- !errors.any?
18
- end
19
-
20
- def value_numeric?
21
- !value.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil?
22
- end
23
-
24
- protected
25
-
26
- def validate
27
- end
28
- end
29
- end
30
- end
@@ -1,16 +0,0 @@
1
- require_relative "base"
2
-
3
- module RailsSettingsUi
4
- module ValueTypes
5
- class Boolean < RailsSettingsUi::ValueTypes::Base
6
- def cast
7
- case value
8
- when 'off'
9
- self.value = false
10
- else
11
- self.value = true
12
- end
13
- end
14
- end
15
- end
16
- end
@@ -1,17 +0,0 @@
1
- require_relative "base"
2
-
3
- module RailsSettingsUi
4
- module ValueTypes
5
- class Duration < RailsSettingsUi::ValueTypes::Base
6
- def cast
7
- value.to_f.seconds
8
- end
9
-
10
- def validate
11
- unless value_numeric?
12
- self.errors << I18n.t("settings.errors.invalid_numeric", default: 'Invalid')
13
- end
14
- end
15
- end
16
- end
17
- end
@@ -1,17 +0,0 @@
1
- require_relative "base"
2
-
3
- module RailsSettingsUi
4
- module ValueTypes
5
- class Fixnum < RailsSettingsUi::ValueTypes::Base
6
- def cast
7
- value.to_i
8
- end
9
-
10
- def validate
11
- unless value_numeric?
12
- self.errors << I18n.t("settings.errors.invalid_numeric", default: 'Invalid')
13
- end
14
- end
15
- end
16
- end
17
- end
@@ -1,17 +0,0 @@
1
- require_relative "base"
2
-
3
- module RailsSettingsUi
4
- module ValueTypes
5
- class Float < RailsSettingsUi::ValueTypes::Base
6
- def cast
7
- value.to_f
8
- end
9
-
10
- def validate
11
- unless value_numeric?
12
- self.errors << I18n.t("settings.errors.invalid_numeric", default: 'Invalid')
13
- end
14
- end
15
- end
16
- end
17
- end
@@ -1,19 +0,0 @@
1
- require_relative "base"
2
-
3
- module RailsSettingsUi
4
- module ValueTypes
5
- class Hash < RailsSettingsUi::ValueTypes::Base
6
- def cast
7
- value
8
- end
9
-
10
- def validate
11
- begin
12
- self.value = JSON.parse(value.gsub(/\=\>/, ':'))
13
- rescue JSON::ParserError => e
14
- self.errors << I18n.t("settings.errors.invalid_hash", default: 'Invalid')
15
- end
16
- end
17
- end
18
- end
19
- end
@@ -1,11 +0,0 @@
1
- require_relative "base"
2
-
3
- module RailsSettingsUi
4
- module ValueTypes
5
- class String < RailsSettingsUi::ValueTypes::Base
6
- def cast
7
- value.to_s
8
- end
9
- end
10
- end
11
- end
@@ -1,11 +0,0 @@
1
- require_relative "base"
2
-
3
- module RailsSettingsUi
4
- module ValueTypes
5
- class Symbol < RailsSettingsUi::ValueTypes::Base
6
- def cast
7
- value.to_sym
8
- end
9
- end
10
- end
11
- end
@@ -1,33 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe RailsSettingsUi::TypeConverter do
4
- describe "default setting value type unknown" do
5
- it "should raise exception" do
6
- Settings.defaults[:project_name] = nil
7
- expect do
8
- RailsSettingsUi::TypeConverter.cast({project_name: :test})
9
- end.to raise_error(RailsSettingsUi::UnknownDefaultValueType)
10
- Settings.defaults[:project_name] = "Dummy"
11
- end
12
- end
13
-
14
- describe "if setting not passed" do
15
- describe "and default value type is boolean" do
16
- it "should to set false" do
17
- Settings.defaults[:check_something] = true
18
- settings = RailsSettingsUi::TypeConverter.cast({})
19
- expect(settings[:check_something]).to be_falsey
20
- end
21
- end
22
- end
23
-
24
- describe ".cast" do
25
- it "should cast settings value by default setting type" do
26
- Settings.defaults[:project_name] = :foo
27
- settings = RailsSettingsUi::TypeConverter.cast({project_name: "bar", limit: "100"})
28
- expect(settings[:project_name]).to equal(:bar)
29
- expect(settings[:limit]).to equal(100)
30
- Settings.defaults[:project_name] = "Dummy"
31
- end
32
- end
33
- end
@@ -1,21 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe RailsSettingsUi::ValueTypes::Array do
4
- describe "#cast" do
5
- it "should cast hash as array of keys as symbols" do
6
- array_type = RailsSettingsUi::ValueTypes::Array.new({auto: "on", manual: "on"})
7
- expect(array_type.cast).to eq([:auto, :manual])
8
- end
9
-
10
- it "if setting presented as select should cast select value as symbol" do
11
- array_type = RailsSettingsUi::ValueTypes::Array.new("auto")
12
- expect(array_type.cast).to eq(:auto)
13
- end
14
-
15
- it "should accept ActionController::Parameters" do
16
- parameter = ActionController::Parameters.new('stuff' => 'on', 'other'=> 'off')
17
- array_type = RailsSettingsUi::ValueTypes::Array.new parameter
18
- expect(array_type.cast).to eq([:stuff])
19
- end
20
- end
21
- end
@@ -1,23 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe RailsSettingsUi::ValueTypes::Base do
4
- describe "if value have any errors" do
5
- it "should not be valid" do
6
- base_type = RailsSettingsUi::ValueTypes::Base.new("test")
7
- base_type.errors << "Invalid"
8
- expect(base_type.valid?).to be_falsey
9
- end
10
- end
11
-
12
- describe "#value_numeric?" do
13
- it "if value is numeric should return true" do
14
- base_type = RailsSettingsUi::ValueTypes::Base.new(349)
15
- expect(base_type.value_numeric?).to be_truthy
16
- end
17
-
18
- it "if value is not numeric should return false" do
19
- base_type = RailsSettingsUi::ValueTypes::Base.new("str")
20
- expect(base_type.value_numeric?).to be_falsey
21
- end
22
- end
23
- end
@@ -1,10 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe RailsSettingsUi::ValueTypes::Boolean do
4
- describe "#cast" do
5
- it "should cast to true" do
6
- boolean_type = RailsSettingsUi::ValueTypes::Boolean.new("on")
7
- expect(boolean_type.cast).to eq(true)
8
- end
9
- end
10
- end
@@ -1,19 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe RailsSettingsUi::ValueTypes::Duration do
4
- describe "#cast" do
5
- it "should cast string to duration" do
6
- param = "#{5.hours}"
7
- fixnum_type = RailsSettingsUi::ValueTypes::Duration.new(param)
8
- expect(fixnum_type.cast).to eq(5.hours)
9
- expect(fixnum_type.cast).to be_instance_of(ActiveSupport::Duration)
10
- end
11
- end
12
-
13
- describe "if value not valid duration" do
14
- it "should be not valid" do
15
- fixnum_type = RailsSettingsUi::ValueTypes::Duration.new("one hour")
16
- expect(fixnum_type.valid?).to be_falsey
17
- end
18
- end
19
- end
@@ -1,17 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe RailsSettingsUi::ValueTypes::Fixnum do
4
- describe "#cast" do
5
- it "should cast to integer" do
6
- fixnum_type = RailsSettingsUi::ValueTypes::Fixnum.new("29")
7
- expect(fixnum_type.cast).to eq(29)
8
- end
9
- end
10
-
11
- describe "if value not numeric" do
12
- it "should be not valid" do
13
- fixnum_type = RailsSettingsUi::ValueTypes::Fixnum.new("qwerty")
14
- expect(fixnum_type.valid?).to be_falsey
15
- end
16
- end
17
- end
@@ -1,17 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe RailsSettingsUi::ValueTypes::Float do
4
- describe "#cast" do
5
- it "should cast to float" do
6
- fixnum_type = RailsSettingsUi::ValueTypes::Float.new("99.21")
7
- expect(fixnum_type.cast).to eq(99.21)
8
- end
9
- end
10
-
11
- describe "if value not numeric" do
12
- it "should be not valid" do
13
- fixnum_type = RailsSettingsUi::ValueTypes::Float.new("qwerty")
14
- expect(fixnum_type.valid?).to be_falsey
15
- end
16
- end
17
- end
@@ -1,26 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe RailsSettingsUi::ValueTypes::Hash do
4
- describe "#cast" do
5
- it "should cast string to hash" do
6
- param = "{\"border_color\"=>\"e0e0e0\", \"block_color\"=>\"ffffff\", \"title\"=>{\"font\"=>\"Tahoma\", \"size\"=>\"12\", \"color\"=>\"107821\"}}"
7
- fixnum_type = RailsSettingsUi::ValueTypes::Hash.new(param)
8
- expect(fixnum_type.cast).to eq({
9
- "border_color" => "e0e0e0",
10
- "block_color" => "ffffff",
11
- "title" => {
12
- "font" => "Tahoma",
13
- "size" => "12",
14
- "color" => "107821"
15
- }
16
- })
17
- end
18
- end
19
-
20
- describe "if value not valid hash" do
21
- it "should be not valid" do
22
- fixnum_type = RailsSettingsUi::ValueTypes::Hash.new("{\"border_color\"=>, \"block_color\"=>\"ffffff\"")
23
- expect(fixnum_type.valid?).to be_falsey
24
- end
25
- end
26
- end