rails-settings-ui 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -0
  3. data/Rakefile +11 -10
  4. data/app/assets/stylesheets/rails_settings_ui/application.css.scss +2 -2
  5. data/app/controllers/rails_settings_ui/settings_controller.rb +1 -1
  6. data/app/helpers/rails_settings_ui/settings_helper.rb +32 -63
  7. data/lib/rails-settings-ui.rb +1 -1
  8. data/lib/rails-settings-ui/engine.rb +7 -0
  9. data/lib/rails-settings-ui/type_converter.rb +58 -0
  10. data/lib/rails-settings-ui/value_types/array.rb +17 -0
  11. data/lib/rails-settings-ui/value_types/base.rb +30 -0
  12. data/lib/rails-settings-ui/value_types/boolean.rb +11 -0
  13. data/lib/rails-settings-ui/value_types/fixnum.rb +17 -0
  14. data/lib/rails-settings-ui/value_types/float.rb +17 -0
  15. data/lib/rails-settings-ui/value_types/hash.rb +19 -0
  16. data/lib/rails-settings-ui/value_types/string.rb +11 -0
  17. data/lib/rails-settings-ui/value_types/symbol.rb +11 -0
  18. data/lib/rails-settings-ui/version.rb +1 -1
  19. data/spec/dummy/README.rdoc +28 -0
  20. data/spec/dummy/Rakefile +6 -0
  21. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  22. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  23. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  24. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  25. data/spec/dummy/app/models/settings.rb +16 -0
  26. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  27. data/spec/dummy/bin/bundle +3 -0
  28. data/spec/dummy/bin/rails +4 -0
  29. data/spec/dummy/bin/rake +4 -0
  30. data/spec/dummy/config.ru +4 -0
  31. data/spec/dummy/config/application.rb +28 -0
  32. data/spec/dummy/config/boot.rb +5 -0
  33. data/spec/dummy/config/database.yml +24 -0
  34. data/spec/dummy/config/environment.rb +5 -0
  35. data/spec/dummy/config/environments/development.rb +29 -0
  36. data/spec/dummy/config/environments/production.rb +80 -0
  37. data/spec/dummy/config/environments/test.rb +36 -0
  38. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  39. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  40. data/spec/dummy/config/initializers/inflections.rb +16 -0
  41. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  42. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  43. data/spec/dummy/config/initializers/session_store.rb +3 -0
  44. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  45. data/spec/dummy/config/locales/en.yml +23 -0
  46. data/spec/dummy/config/routes.rb +3 -0
  47. data/spec/dummy/db/migrate/20140320182332_create_settings.rb +17 -0
  48. data/spec/dummy/db/schema.rb +27 -0
  49. data/spec/dummy/db/test.sqlite3 +0 -0
  50. data/spec/dummy/log/development.log +264 -0
  51. data/spec/dummy/log/test.log +3237 -0
  52. data/spec/dummy/public/404.html +58 -0
  53. data/spec/dummy/public/422.html +58 -0
  54. data/spec/dummy/public/500.html +57 -0
  55. data/spec/dummy/public/favicon.ico +0 -0
  56. data/spec/dummy/tmp/cache/assets/test/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  57. data/spec/dummy/tmp/cache/assets/test/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  58. data/spec/dummy/tmp/cache/assets/test/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  59. data/spec/dummy/tmp/capybara/capybara-201403251000145108838835.html +68 -0
  60. data/spec/dummy/tmp/capybara/capybara-201403251029034025293240.html +95 -0
  61. data/spec/features/settings_spec.rb +63 -0
  62. data/spec/lib/rails-settings-ui/type_converter_spec.rb +33 -0
  63. data/spec/lib/rails-settings-ui/value_types/array_spec.rb +15 -0
  64. data/spec/lib/rails-settings-ui/value_types/base_spec.rb +23 -0
  65. data/spec/lib/rails-settings-ui/value_types/boolean_spec.rb +10 -0
  66. data/spec/lib/rails-settings-ui/value_types/fixnum_spec.rb +17 -0
  67. data/spec/lib/rails-settings-ui/value_types/float_spec.rb +17 -0
  68. data/spec/lib/rails-settings-ui/value_types/hash_spec.rb +26 -0
  69. data/spec/lib/rails-settings-ui/value_types/string_spec.rb +10 -0
  70. data/spec/lib/rails-settings-ui/value_types/symbol_spec.rb +10 -0
  71. data/spec/spec_helper.rb +27 -0
  72. metadata +175 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 23b3fb25f9bb4d3f397e71a0e65358869ca63f2b
4
- data.tar.gz: 6ed56614f5e2f6eea5d97c4414d14f9ee2580ad2
3
+ metadata.gz: 1bd37d900e097cdee839f64abed32539038d2c4f
4
+ data.tar.gz: 58a2060c7fc0c73ed046a811386dfd09da345c91
5
5
  SHA512:
6
- metadata.gz: da0612bdb5060da129a2092c55d71e861ae8eebae4fa819d8dcfd670ea83d84c425c95fababd39c29c71b3ef613035257d0a7c08963a53a3bf5ae606f36c1a05
7
- data.tar.gz: 407a41786a292a92209256efecba8603353d5418dba7dcce8c9e73e11bfa6afb3153fd00aec2e1b755420c21aaf4f7179142f9d74ec588a6108fe6ebd50d9900
6
+ metadata.gz: 97867588d71608cd5354396f6162d1b93c512327db2f90306cfe60d7e665cadf5646800265302b866529a874f1e7deb48572133406b24529c237f004893352e4
7
+ data.tar.gz: 73c09c67f469370b1dd8f29e0a385fc47bf7d69f874319e35b6fa04857b81a65e65431a04c9e45982818b075b94cef55493fb304f377eee02ca13be6d38fbe2b
data/Gemfile CHANGED
@@ -4,4 +4,6 @@ gemspec
4
4
 
5
5
  group :development, :test do
6
6
  gem "sqlite3"
7
+ gem "rails-settings-cached"
8
+ gem "haml-rails"
7
9
  end
data/Rakefile CHANGED
@@ -6,16 +6,17 @@ rescue LoadError
6
6
  puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
7
  end
8
8
 
9
- require 'rake'
10
- require 'rake/rdoctask'
9
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
10
+ load 'rails/tasks/engine.rake'
11
11
 
12
- require 'rake/testtask'
12
+ Bundler::GemHelper.install_tasks
13
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
14
+ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
15
+
16
+ require 'rspec/core'
17
+ require 'rspec/core/rake_task'
18
+
19
+ desc "Run all specs in spec directory (excluding plugin specs)"
20
+ RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
20
21
 
21
- task :default => :test
22
+ task :default => :spec
@@ -1,4 +1,4 @@
1
1
  /*
2
- *= require bootstrap_and_overrides
2
+ *= require ./bootstrap_and_overrides
3
3
  *= require_self
4
- */
4
+ */
@@ -23,6 +23,6 @@ class RailsSettingsUi::SettingsController < RailsSettingsUi::ApplicationControll
23
23
  end
24
24
 
25
25
  def cast_settings_params
26
- @casted_settings = RailsSettingsUi::SettingsHelper.cast(params["settings"])
26
+ @casted_settings = RailsSettingsUi::TypeConverter.cast(params["settings"])
27
27
  end
28
28
  end
@@ -1,75 +1,44 @@
1
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
2
  def setting_field(setting_name, setting_value, all_settings)
46
3
  if RailsSettingsUi.settings_displayed_as_select_tag.include?(setting_name.to_sym)
47
- default_setting_values = I18n.t("settings.attributes.#{setting_name}.labels", default: {}).map do |label, value|
48
- [label, value]
49
- end
50
- select_tag("settings[#{setting_name.to_s}]", options_for_select(default_setting_values, setting_value))
4
+ select_tag_field(setting_name, setting_value)
51
5
  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, all_settings[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
6
+ checkboxes_group_field(setting_name, all_settings)
58
7
  elsif [TrueClass, FalseClass].include?(setting_value.class)
59
- return check_box_tag("settings[#{setting_name.to_s}]", nil, setting_value).html_safe
8
+ checkbox_field(setting_name, setting_value)
60
9
  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
10
+ text_field(setting_name, setting_value)
11
+ end
12
+ end
66
13
 
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'))
14
+ def select_tag_field(setting_name, setting_value)
15
+ default_setting_values = I18n.t("settings.attributes.#{setting_name}.labels", default: {}).map do |label, value|
16
+ [label, value]
69
17
  end
18
+ select_tag("settings[#{setting_name.to_s}]", options_for_select(default_setting_values, setting_value))
70
19
  end
71
20
 
72
- def self.is_numeric?(value)
73
- !value.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil?
21
+ def checkboxes_group_field(setting_name, all_settings)
22
+ field = ""
23
+ Settings.defaults[setting_name.to_sym].each do |value|
24
+ field << check_box_tag("settings[#{setting_name.to_s}][#{value.to_s}]", nil, all_settings[setting_name.to_s].include?(value), style: "margin: 0 10px;")
25
+ 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;")
26
+ end
27
+ field.html_safe
28
+ end
29
+
30
+ def checkbox_field(setting_name, setting_value)
31
+ check_box_tag("settings[#{setting_name.to_s}]", nil, setting_value).html_safe
32
+ end
33
+
34
+ def text_field(setting_name, setting_value)
35
+ field = if setting_value.to_s.size > 30
36
+ text_area_tag("settings[#{setting_name}]", setting_value.to_s, rows: 10)
37
+ else
38
+ text_field_tag("settings[#{setting_name}]", setting_value.to_s)
39
+ end
40
+
41
+ help_block_content = I18n.t("settings.attributes.#{setting_name}.help_block", default: '')
42
+ field + (help_block_content.presence && content_tag(:span, help_block_content, class: 'help-block'))
74
43
  end
75
44
  end
@@ -3,7 +3,7 @@ require 'rails-settings-ui/engine'
3
3
  require 'rails-settings-ui/main_app_route_delegator'
4
4
  require 'rails-settings-ui/version'
5
5
 
6
- require 'rails-settings-cached'
6
+ require "rails-settings-ui/type_converter"
7
7
 
8
8
  module RailsSettingsUi
9
9
  mattr_accessor :parent_controller
@@ -1,5 +1,12 @@
1
1
  module ::RailsSettingsUi
2
2
  class Engine < Rails::Engine
3
3
  isolate_namespace RailsSettingsUi
4
+
5
+ config.generators do |g|
6
+ g.test_framework :rspec, fixture: false
7
+ g.fixture_replacement :factory_girl, dir: 'spec/factories'
8
+ g.assets false
9
+ g.helper false
10
+ end
4
11
  end
5
12
  end
@@ -0,0 +1,58 @@
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/float"
6
+ require_relative "value_types/array"
7
+ require_relative "value_types/boolean"
8
+
9
+ module RailsSettingsUi
10
+ class UnknownDefaultValueType < StandardError;end
11
+
12
+ class TypeConverter
13
+ VALUE_TYPES_MAP = {
14
+ String => RailsSettingsUi::ValueTypes::String,
15
+ Symbol => RailsSettingsUi::ValueTypes::Symbol,
16
+ Fixnum => RailsSettingsUi::ValueTypes::Fixnum,
17
+ ActiveSupport::HashWithIndifferentAccess => RailsSettingsUi::ValueTypes::Hash,
18
+ Float => RailsSettingsUi::ValueTypes::Float,
19
+ Array => RailsSettingsUi::ValueTypes::Array,
20
+ FalseClass => RailsSettingsUi::ValueTypes::Boolean,
21
+ TrueClass => RailsSettingsUi::ValueTypes::Boolean
22
+ }
23
+
24
+ def self.cast(settings)
25
+ errors = {}
26
+ settings.each do |name, value|
27
+ type = setting_value_type(name, value)
28
+ settings[name] = type.cast
29
+ if type.errors.any?
30
+ errors[name.to_sym] = type.errors
31
+ end
32
+ end
33
+ settings = set_non_presented_boolean_settings_to_false(settings)
34
+
35
+ settings[:errors] = errors
36
+ settings
37
+ end
38
+
39
+ def self.setting_value_type(name, value)
40
+ default_setting_value_type = Settings.defaults[name.to_sym].class
41
+ unless VALUE_TYPES_MAP.keys.include?(default_setting_value_type)
42
+ raise RailsSettingsUi::UnknownDefaultValueType, "Unknown default value type #{default_setting_value_type}"
43
+ end
44
+
45
+ setting_value_type_class = VALUE_TYPES_MAP[default_setting_value_type]
46
+ setting_value_type_class.new(value)
47
+ end
48
+
49
+ def self.set_non_presented_boolean_settings_to_false(settings)
50
+ Settings.defaults.each do |name, value|
51
+ if !settings[name.to_sym].present? && [TrueClass, FalseClass].include?(value.class)
52
+ settings[name.to_sym] = false
53
+ end
54
+ end
55
+ settings
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,17 @@
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
+ # or in select tag
11
+ else
12
+ value.to_sym
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,30 @@
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
@@ -0,0 +1,11 @@
1
+ require_relative "base"
2
+
3
+ module RailsSettingsUi
4
+ module ValueTypes
5
+ class Boolean < RailsSettingsUi::ValueTypes::Base
6
+ def cast
7
+ self.value = true
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
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
@@ -0,0 +1,17 @@
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
@@ -0,0 +1,19 @@
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
@@ -0,0 +1,11 @@
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
@@ -0,0 +1,11 @@
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,3 +1,3 @@
1
1
  module RailsSettingsUi
2
- VERSION = '0.0.2'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks