activeadmin_settings_cached 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3942b06c09451668f69cdebc55fd60ba0fbc06fc
4
- data.tar.gz: 82244fce68d448d8b9a42d25da86c5d00473afda
3
+ metadata.gz: 66b215e385891ca9912114999fb2f1503e0ea964
4
+ data.tar.gz: 144d18fd399db01299913ffde456ba1d758dd875
5
5
  SHA512:
6
- metadata.gz: 5433274987531047bae38d9459083252691675114dde5bd271d3c9d4be6c07b8a6ec337dd726d7c92305017524e757b00763138a5b616e0e97500715aa5aadac
7
- data.tar.gz: 37593953667ae4262a21fe5025bce352839f813f2c503a9932076670121c209075c68d3453e92a4ddb6f7a9f233c6fda7890cf762db20f28aa03a3a810d495eb
6
+ metadata.gz: 86461b1090024fbbd1be6754c99de2f3e8f31bffeb9bf80d099227063052db47f8a7fc36e996197cbcdb2622252b3c63cb7f7e1bb14cc1950f16229c46ae09ff
7
+ data.tar.gz: bee99e56e562d814c492c1055052b9895bd37465b07780a52e662e275590568fd2bf995148d50fe1378937ebdd39f91b3438f17d24e5e7169f7447557c4598d5
data/.gitignore CHANGED
@@ -15,3 +15,4 @@
15
15
  .ruby-version
16
16
  .idea
17
17
  mkmf.log
18
+ /gemfiles
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml CHANGED
@@ -1,11 +1,10 @@
1
+ language: ruby
2
+ cache: bundler
3
+
1
4
  script:
2
- - bundle exec rspec spec
5
+ - make
6
+
3
7
  rvm:
4
- - 2.0.0
5
- - 2.1.5
6
- - 2.2.0
8
+ #- 2.1.5
9
+ #- 2.2.0
7
10
  - 2.3.0
8
- before_install:
9
- - gem install bundler -v '= 1.9.3'
10
- - gem update --system
11
- - gem --version
data/Appraisals ADDED
@@ -0,0 +1,11 @@
1
+ appraise "rails4.2" do
2
+ gem "rails", "~> 4.2.0"
3
+ end
4
+
5
+ #appraise "rails5.0" do
6
+ #gem "rails", "~> 5.0.0"
7
+ #gem 'inherited_resources', github: 'activeadmin/inherited_resources'
8
+ #gem 'ransack', github: 'activerecord-hackery/ransack'
9
+ #gem 'draper', '> 3.x'
10
+ #gem 'sass-rails', github: 'rails/sass-rails' # For Sprockets 4
11
+ #end
data/Dockerfile ADDED
@@ -0,0 +1,26 @@
1
+ FROM ruby:2.3-slim
2
+
3
+ ENV PHANTOMJS_VERSION 1.9.8
4
+
5
+ RUN apt-get update -qq && \
6
+ DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
7
+ make \
8
+ gcc \
9
+ g++ \
10
+ libsqlite3-dev \
11
+ git-core \
12
+ wget \
13
+ libfreetype6 \
14
+ libfontconfig \
15
+ bzip2 && \
16
+ apt-get clean
17
+
18
+ RUN \
19
+ wget -q --no-check-certificate -O /tmp/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2 https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2 && \
20
+ mkdir -p /srv/var && \
21
+ tar -xjf /tmp/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2 -C /tmp && \
22
+ rm -f /tmp/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2 && \
23
+ mv /tmp/phantomjs-$PHANTOMJS_VERSION-linux-x86_64/ /srv/var/phantomjs && \
24
+ ln -s /srv/var/phantomjs/bin/phantomjs /usr/bin/phantomjs
25
+
26
+ WORKDIR /app
data/Gemfile CHANGED
@@ -1,8 +1,9 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  group :test do
4
- #gem 'turbolinks'
5
- gem 'activeadmin', github: 'activeadmin/activeadmin', branch: 'ab1d8d1a4a1af748ae7009d9d725193d780d0059'
4
+ gem 'pry-byebug'
5
+ gem 'therubyracer'
6
+ gem 'activeadmin', github: 'activeadmin/activeadmin'
6
7
  end
7
8
 
8
9
  gemspec
data/Makefile CHANGED
@@ -1,3 +1,16 @@
1
- all:
1
+ default: test
2
+
3
+ test: appraisals
4
+ bundle exec appraisal rspec ${T}
5
+
6
+ appraisals: setup
7
+ bundle exec appraisal install
8
+
9
+ setup:
10
+ gem install bundler --no-ri --no-rdoc
11
+ bundle check || bundle install -j 2
12
+
13
+ clean:
14
+ rm -f Gemfile.lock
2
15
  rm -rf spec/rails
3
- bundle exec rspec
16
+ rm -rf gemfiles
data/README.md CHANGED
@@ -20,7 +20,7 @@ And then execute:
20
20
 
21
21
  Create your settings model:
22
22
 
23
- $ rails g settings Settings
23
+ $ rails g settings:install
24
24
  $ bundle exec rake db:migrate
25
25
 
26
26
  Create your settings page:
@@ -52,7 +52,8 @@ end
52
52
  Tool | Description
53
53
  --------------------- | -----------
54
54
  :model_name |String, settings model name override (default: uses name from global config.)
55
- :starting_with |String, each key must starting with, (default: nil)
55
+ :starting_with |String, each key must starting with, (default: nil)
56
+ :key |String, Symbol root key, can be replacement for starting_with, (default: nil)
56
57
  :template |String, custom template rendering (default: 'admin/settings/index')
57
58
  :template_object |object passing to view (default: ActiveadminSettingsCached::Model instance)
58
59
  :display |Hash, display settings override (default: {})
@@ -19,12 +19,13 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  s.add_dependency 'activeadmin'
22
- s.add_dependency 'rails-settings-cached', '>= 0.5.3', '< 0.5.5'
22
+ s.add_dependency 'rails-settings-cached', '>= 0.5.3', '< 0.6.6'
23
+ s.add_dependency 'dry-types', '>= 0.8.1'
23
24
 
24
25
  s.add_development_dependency "bundler"
25
26
  s.add_development_dependency "rake"
26
27
  s.add_development_dependency "coveralls"
27
-
28
+ s.add_development_dependency "appraisal"
28
29
  s.add_development_dependency "database_cleaner"
29
30
  s.add_development_dependency "sqlite3"
30
31
  s.add_development_dependency "rspec-rails"
@@ -9,11 +9,12 @@
9
9
  </thead>
10
10
  <tbody>
11
11
  <% settings_model.settings.each_pair do |name, _| %>
12
+ <% field_name = settings_model.field_name(name) %>
12
13
  <tr class="odd">
13
- <td><strong><%= f.label t("settings.attributes.#{name}", default: name) %></strong></td>
14
+ <td><strong><%= f.label t("settings.attributes.#{field_name}", default: field_name) %></strong></td>
14
15
  <td>
15
16
  <div class='form'>
16
- <ol><%= f.input name, settings_model.field_options(name) %></ol>
17
+ <ol><%= f.input field_name, settings_model.field_options(field_name, name) %></ol>
17
18
  </div>
18
19
  </td>
19
20
  </tr>
@@ -0,0 +1,16 @@
1
+ de:
2
+ activeadmin_settings_cached:
3
+ settings:
4
+ update:
5
+ success: Konfiguration wurde gespeichert.
6
+
7
+ activerecord:
8
+ models:
9
+ settings: 'Konfiguration'
10
+
11
+ settings:
12
+ menu:
13
+ label: 'Konfiguration'
14
+ index:
15
+ name: 'Name'
16
+ value: 'Wert'
@@ -0,0 +1,13 @@
1
+ version: '2'
2
+
3
+ services:
4
+ app:
5
+ build: '.'
6
+ command: 'make test'
7
+ volumes:
8
+ - '.:/app'
9
+ - gems:/usr/local/bundle
10
+
11
+ volumes:
12
+ gems:
13
+ external: true
@@ -5,7 +5,7 @@ module ActiveadminSettingsCached
5
5
  attr_accessor :model_name, :display
6
6
 
7
7
  def model_name
8
- (@model_name ||= 'Settings').constantize
8
+ (@model_name ||= 'Setting').constantize
9
9
  end
10
10
 
11
11
  def display
@@ -1,17 +1,15 @@
1
+ require 'dry-types'
2
+
1
3
  module ActiveadminSettingsCached
4
+ # Coerce user input values to defined types
5
+ #
6
+ # @api private
2
7
  class Coercions
3
- SIMPLE_COERCIONS = {
4
- float: :to_f,
5
- integer: :to_i,
6
- symbol: :to_sym
7
- }
8
8
  attr_reader :defaults, :display
9
9
 
10
10
  def initialize(defaults, display)
11
11
  @defaults = defaults
12
12
  @display = display
13
-
14
- init_methods
15
13
  end
16
14
 
17
15
  def cast_params(params)
@@ -22,39 +20,36 @@ module ActiveadminSettingsCached
22
20
  return coerced_params unless block_given?
23
21
 
24
22
  coerced_params.each do |name, value|
25
- yield(name, value.call)
23
+ yield(name, value.call) unless value.nil?
26
24
  end
27
25
  end
28
26
 
29
27
  private
30
28
 
31
- def coerce(m, &block)
32
- define_singleton_method :"string_to_#{m.to_s}", &block
33
- end
34
-
35
- def init_methods
36
- coerce :boolean do |value|
37
- value && %w(true 1 yes y t).include?(value)
38
- end
39
-
40
- SIMPLE_COERCIONS.each do |k, v|
41
- coerce(k) { |value| String(value).send(v) }
42
- end
43
- end
44
-
45
29
  def cast_value(name, value)
46
30
  case defaults[name]
47
31
  when TrueClass, FalseClass
48
- display[name].to_s == 'boolean' ? -> { string_to_boolean(value) } : -> { value }
32
+ -> { value_or_default('bool', value, false) }
49
33
  when Integer
50
- -> { string_to_integer(value) }
34
+ -> { value_or_default('int', value, 0) }
51
35
  when Float
52
- -> { string_to_float(value) }
36
+ -> { value_or_default('float', value, 0.0) }
37
+ when Hash, 'ActiveSupport::HashWithIndifferentAccess'
38
+ nil
53
39
  when Symbol
54
- -> { string_to_symbol(value) }
40
+ -> { value.to_sym }
55
41
  else
56
42
  -> { value }
57
43
  end
58
44
  end
45
+
46
+ def value_or_default(type, value, default)
47
+ result = Dry::Types["form.#{type}"].call(value)
48
+ if Dry::Types[type].valid?(result)
49
+ result
50
+ else
51
+ default
52
+ end
53
+ end
59
54
  end
60
55
  end
@@ -1,30 +1,33 @@
1
1
  module ActiveadminSettingsCached
2
2
  module DSL
3
- # Declares settings function
3
+ # Declares settings function.
4
4
  #
5
- # Options:
6
- # +model_name+:: settings model name override (default: uses name from global config.)
7
- # +starting_with+:: each key must starting with, (default: nil)
8
- # +template+:: custom template rendering (default: 'admin/settings/index')
9
- # +template_object+:: object to use in templates (default: ActiveadminSettingsCached::Model instance)
10
- # +display+:: display settings override (default: nil)
11
- # +title+:: title value override (default: I18n.t('settings.menu.label'))
5
+ # @api public
12
6
  #
7
+ # @param [Hash] options
8
+ # @option options [String] :model_name, settings model name override (default: uses name from global config.)
9
+ # @option options [String] :starting_with, each key must starting with, (default: nil)
10
+ # @option options [String] :key, root key can be replacement for starting_with, (default: nil)
11
+ # @option options [String] :tempalte custom, template rendering (default: 'admin/settings/index')
12
+ # @option options [String] :template_object, object to use in templates (default: ActiveadminSettingsCached::Model instance)
13
+ # @option options [String] :display, display settings override (default: nil)
14
+ # @option options [String] :title, title value override (default: I18n.t('settings.menu.label'))
13
15
  def active_admin_settings_page(options = {}, &block)
14
- options.assert_valid_keys(*Options::VALID_OPTIONS)
16
+ options.assert_valid_keys(*ActiveadminSettingsCached::Options::VALID_OPTIONS)
15
17
 
16
- options = Options.options_for(options)
17
- coercion = Coercions.new(options[:template_object].defaults, options[:template_object].display)
18
+ options = ActiveadminSettingsCached::Options.options_for(options)
19
+ coercion =
20
+ ActiveadminSettingsCached::Coercions.new(options[:template_object].defaults, options[:template_object].display)
18
21
 
19
22
  content title: options[:title] do
20
23
  render partial: options[:template], locals: { settings_model: options[:template_object] }
21
24
  end
22
25
 
23
26
  page_action :update, method: :post do
24
- settings_params = params.require(:settings).permit(options[:template_object].defaults.keys)
27
+ settings_params = params.require(:settings).permit!
25
28
 
26
29
  coercion.cast_params(settings_params) do |name, value|
27
- options[:template_object][name] = value
30
+ options[:template_object].save(name, value)
28
31
  end
29
32
 
30
33
  flash[:success] = t('activeadmin_settings_cached.settings.update.success'.freeze)
@@ -7,7 +7,7 @@ module ActiveadminSettingsCached
7
7
  config.autoload_paths += Dir["#{config.root}/lib"]
8
8
 
9
9
  initializer 'activeadmin_settings_cached' do
10
- ActiveAdmin::DSL.send(:include, ActiveadminSettingsCached::DSL)
10
+ ::ActiveAdmin::DSL.send(:include, ::ActiveadminSettingsCached::DSL)
11
11
  end
12
12
  end
13
13
  end
@@ -1,6 +1,6 @@
1
1
  module ActiveadminSettingsCached
2
2
  class Model
3
- include ActiveModel::Model
3
+ include ::ActiveModel::Model
4
4
 
5
5
  attr_reader :attributes
6
6
 
@@ -11,9 +11,13 @@ module ActiveadminSettingsCached
11
11
  assign_attributes(merge_attributes(args))
12
12
  end
13
13
 
14
- def field_options(settings_name)
14
+ def field_name(settings_name)
15
+ has_key? ? "#{attributes[:key]}.#{settings_name}" : settings_name
16
+ end
17
+
18
+ def field_options(settings_name, key_name)
15
19
  default_value = defaults[settings_name]
16
- value = settings[settings_name]
20
+ value = settings[key_name]
17
21
 
18
22
  input_opts = if default_value.is_a?(Array)
19
23
  {
@@ -36,12 +40,22 @@ module ActiveadminSettingsCached
36
40
  end
37
41
 
38
42
  def settings
39
- data = load_settings
40
- ActiveSupport::OrderedHash[data.to_a.sort { |a, b| a.first <=> b.first }] if data
43
+ data = has_key? ? load_settings_by_key : load_settings
44
+ return unless data
45
+
46
+ ::ActiveSupport::OrderedHash[data.to_a.sort { |a, b| a.first <=> b.first }]
41
47
  end
42
48
 
43
49
  def defaults
44
- settings_model.defaults
50
+ settings_model.respond_to?(:defaults) ?
51
+ settings_model.defaults :
52
+ ::RailsSettings::Default
53
+ end
54
+
55
+ def defaults_keys
56
+ settings_model.respond_to?(:defaults) ?
57
+ settings_model.defaults.keys :
58
+ ::RailsSettings::Default.instance.keys
45
59
  end
46
60
 
47
61
  def display
@@ -56,6 +70,14 @@ module ActiveadminSettingsCached
56
70
  settings_model[param] = value
57
71
  end
58
72
 
73
+ def save(key, value)
74
+ if has_key?
75
+ settings_model.merge!(attributes[:key], { clean_key(key) => value })
76
+ else
77
+ self[key] = value
78
+ end
79
+ end
80
+
59
81
  def persisted?
60
82
  false
61
83
  end
@@ -68,6 +90,18 @@ module ActiveadminSettingsCached
68
90
  settings_model.public_send(meth, attributes[:starting_with])
69
91
  end
70
92
 
93
+ def load_settings_by_key
94
+ self[attributes[:key]]
95
+ end
96
+
97
+ def has_key?
98
+ attributes[:key].present?
99
+ end
100
+
101
+ def clean_key(key)
102
+ key.is_a?(Symbol) ? key : "#{key.sub("#{attributes[:key]}.", '')}"
103
+ end
104
+
71
105
  def assign_attributes(args = {})
72
106
  @attributes.merge!(args)
73
107
  end
@@ -75,8 +109,9 @@ module ActiveadminSettingsCached
75
109
  def default_attributes
76
110
  {
77
111
  starting_with: nil,
78
- model_name: ActiveadminSettingsCached.config.model_name,
79
- display: ActiveadminSettingsCached.config.display
112
+ key: nil,
113
+ model_name: ::ActiveadminSettingsCached.config.model_name,
114
+ display: ::ActiveadminSettingsCached.config.display
80
115
  }
81
116
  end
82
117
 
@@ -6,11 +6,13 @@ module ActiveadminSettingsCached
6
6
  :template,
7
7
  :template_object,
8
8
  :display,
9
+ :key,
9
10
  :title
10
11
  ].freeze
11
12
 
12
13
  def self.options_for(options = {})
13
- options[:template_object] = ActiveadminSettingsCached::Model.new(options) unless options[:template_object]
14
+ options[:template_object] =
15
+ ::ActiveadminSettingsCached::Model.new(options) unless options[:template_object]
14
16
 
15
17
  {
16
18
  template: 'admin/settings/index',
@@ -1,3 +1,3 @@
1
1
  module ActiveadminSettingsCached
2
- VERSION = '2.0.1'
2
+ VERSION = '2.1.0'
3
3
  end
@@ -1,42 +1,78 @@
1
- require 'spec_helper'
2
-
3
1
  RSpec.describe ActiveadminSettingsCached::Coercions do
4
- let(:display) { Hash['base.first_setting' => 'string', 'base.second_setting' => 'boolean',
5
- 'base.third_setting' => 'number', 'base.four_setting' => 'number',
6
- 'second.first_setting' => 'boolean', 'second.second_setting' => 'string'].with_indifferent_access }
7
- let(:defaults) { Hash['base.first_setting' => 'AAA', 'base.second_setting' => true, 'base.third_setting' => 5,
8
- 'base.four_setting' => 5.5, 'base.five_setting' => :aaa, 'second.first_setting' => false,
9
- 'second.second_setting' => 'BBB'].with_indifferent_access }
10
- let(:right_params) { ActionController::Parameters.new(Hash['base.first_setting' => 'BBB', 'base.second_setting' => 'false', 'base.third_setting' => '155',
11
- 'base.four_setting' => '55.5', 'base.five_setting' => 'bbb', 'second.first_setting' => 'true',
12
- 'second.second_setting' => 'AAA']) }
13
- let(:wrong_params) { ActionController::Parameters.new(Hash['base.second_setting' => 'hjgj', 'base.third_setting' => 'fhfh',
14
- 'base.four_setting' => 'gjfhg', 'second.first_setting' => 'ggf']) }
2
+ let(:display) do
3
+ Hash[
4
+ 'base.first_setting' => 'string',
5
+ 'base.second_setting' => 'boolean',
6
+ 'base.third_setting' => 'number',
7
+ 'base.four_setting' => 'number',
8
+ 'second.first_setting' => 'boolean',
9
+ 'second.second_setting' => 'string'
10
+ ].with_indifferent_access
11
+ end
12
+
13
+ let(:defaults) do
14
+ Hash[
15
+ 'base.first_setting' => 'AAA',
16
+ 'base.second_setting' => true,
17
+ 'base.third_setting' => 5,
18
+ 'base.four_setting' => 5.5,
19
+ 'base.five_setting' => :aaa,
20
+ 'second.first_setting' => false,
21
+ 'second.second_setting' => 'BBB',
22
+ 'some' => Hash.new
23
+ ].with_indifferent_access
24
+ end
25
+
26
+ let(:right_params) do
27
+ ActionController::Parameters.new(
28
+ Hash[
29
+ 'base.first_setting' => 'BBB',
30
+ 'base.second_setting' => 'false',
31
+ 'base.third_setting' => '155',
32
+ 'base.four_setting' => '55.5',
33
+ 'base.five_setting' => 'bbb',
34
+ 'second.first_setting' => 'true',
35
+ 'second.second_setting' => 'AAA'
36
+ ]
37
+ )
38
+ end
39
+
40
+ let(:wrong_params) do
41
+ ActionController::Parameters.new(
42
+ Hash['base.second_setting' => 'hjgj',
43
+ 'base.third_setting' => 'fhfh',
44
+ 'base.four_setting' => 'gjfhg',
45
+ 'second.first_setting' => 'ggf',
46
+ 'some' => Hash.new
47
+ ])
48
+ end
49
+
15
50
  let(:no_params) { ActionController::Parameters.new(Hash[]) }
16
51
 
17
- before do
18
- @coercions = ActiveadminSettingsCached::Coercions.new(defaults, display)
52
+ subject(:coercions) do
53
+ ActiveadminSettingsCached::Coercions.new(defaults, display)
19
54
  end
20
55
 
21
56
  it 'when good params' do
22
- expect { |b|
23
- @coercions.cast_params right_params, &b
24
- }.to yield_successive_args(['base.first_setting', 'BBB'], ['base.second_setting', false],
25
- ['base.third_setting', 155], ['base.four_setting', 55.5],
26
- ['base.five_setting', :bbb], ['second.first_setting', true],
27
- ['second.second_setting', 'AAA'])
57
+ expect { |b| coercions.cast_params(right_params, &b) }
58
+ .to yield_successive_args(['base.first_setting', 'BBB'],
59
+ ['base.second_setting', false],
60
+ ['base.third_setting', 155],
61
+ ['base.four_setting', 55.5],
62
+ ['base.five_setting', :bbb],
63
+ ['second.first_setting', true],
64
+ ['second.second_setting', 'AAA'])
28
65
  end
29
66
 
30
67
  it 'when wrong params' do
31
- expect { |b|
32
- @coercions.cast_params wrong_params, &b
33
- }.to yield_successive_args(['base.second_setting', false], ['base.third_setting', 0],
34
- ['base.four_setting', 0.0], ['second.first_setting', false])
68
+ expect { |b| coercions.cast_params(wrong_params, &b) }
69
+ .to yield_successive_args(['base.second_setting', false],
70
+ ['base.third_setting', 0],
71
+ ['base.four_setting', 0.0],
72
+ ['second.first_setting', false])
35
73
  end
36
74
 
37
75
  it 'when no params' do
38
- expect { |b|
39
- @coercions.cast_params no_params, &b
40
- }.not_to yield_control
76
+ expect { |b| coercions.cast_params(no_params, &b) }.not_to yield_control
41
77
  end
42
78
  end
data/spec/model_spec.rb CHANGED
@@ -1,5 +1,3 @@
1
- require 'spec_helper'
2
-
3
1
  RSpec.describe ActiveadminSettingsCached::Model do
4
2
  include ActiveModel::Lint::Tests
5
3
 
@@ -9,6 +7,164 @@ RSpec.describe ActiveadminSettingsCached::Model do
9
7
  end
10
8
  end
11
9
 
10
+ before(:all) do
11
+ Setting.merge!('some', {
12
+ 'first_setting' => 'CCC',
13
+ 'second_setting' => true
14
+ })
15
+ end
16
+
17
+ let(:all_options) do
18
+ {
19
+ model_name: 'Setting',
20
+ starting_with: 'base.',
21
+ key: nil,
22
+ display: {'base.first_setting' => 'string', 'base.second_setting' => 'boolean'}
23
+ }
24
+ end
25
+
26
+ let(:key_options) do
27
+ {
28
+ model_name: 'Setting',
29
+ starting_with: nil,
30
+ key: 'some',
31
+ display: {'some.first_setting' => 'string', 'base.second_setting' => 'boolean'}
32
+ }
33
+ end
34
+
35
+ let(:no_options) do
36
+ {}
37
+ end
38
+
39
+ context '#attributes' do
40
+ it 'set options' do
41
+ object = described_class.new(all_options)
42
+ expect(object.attributes).to eq({
43
+ starting_with: all_options[:starting_with],
44
+ key: nil,
45
+ model_name: Setting,
46
+ display: all_options[:display]
47
+ })
48
+ end
49
+
50
+ it 'set default options' do
51
+ object = described_class.new(no_options)
52
+ expect(object.attributes).to eq({
53
+ starting_with: nil,
54
+ key: nil,
55
+ model_name: Setting,
56
+ display: {}
57
+ })
58
+ end
59
+
60
+ it 'set key options' do
61
+ object = described_class.new(key_options)
62
+ expect(object.attributes).to eq({
63
+ starting_with: nil,
64
+ key: key_options[:key],
65
+ model_name: Setting,
66
+ display: key_options[:display]
67
+ })
68
+ end
69
+ end
70
+
71
+ context '#field_name' do
72
+ it 'with keyed name' do
73
+ object = described_class.new(key_options)
74
+ expect(object.field_name('first_setting')).to eq('some.first_setting')
75
+ end
76
+
77
+ it 'with normal name' do
78
+ object = described_class.new(all_options)
79
+ expect(object.field_name('base.first_setting')).to eq('base.first_setting')
80
+ end
81
+ end
82
+
83
+ context '#field_options' do
84
+ it 'with default element' do
85
+ object = described_class.new(all_options)
86
+ expect(object.field_options('base.first_setting', 'base.first_setting')).to eq({as: 'string',
87
+ label: false,
88
+ input_html: {value: 'AAA', placeholder: 'AAA'}
89
+ })
90
+ end
91
+
92
+ it 'with keyed element' do
93
+ object = described_class.new(key_options)
94
+ expect(object.field_options('some.first_setting', 'first_setting')).to eq({as: 'string',
95
+ label: false,
96
+ input_html: {value: 'CCC', placeholder: 'AAA'}
97
+ })
98
+ end
99
+
100
+ it 'with array element' do
101
+ object = described_class.new(all_options.merge({display: {'base.first_setting' => 'string',
102
+ 'base.second_setting' => 'boolean',
103
+ 'base.six_setting' => 'array'}}))
104
+ expect(object.field_options('base.six_setting', 'base.six_setting')).to eq({as: 'array',
105
+ label: false,
106
+ collection: %w(a b),
107
+ selected: %w(a b)
108
+ })
109
+ end
110
+
111
+ it 'with boolean element' do
112
+ object = described_class.new(all_options)
113
+ expect(object.field_options('base.second_setting', 'base.second_setting')).to eq({as: 'boolean',
114
+ label: '',
115
+ input_html: {checked: true},
116
+ checked_value: 'true',
117
+ unchecked_value: 'false'
118
+ })
119
+ end
120
+ end
121
+
122
+ context '#settings' do
123
+ it 'normal settings' do
124
+ object = described_class.new(all_options)
125
+ expect(object.settings).to eq(Setting.get_all('base.'))
126
+ end
127
+
128
+ it 'settings by key' do
129
+ object = described_class.new(key_options)
130
+ expect(object.settings).to eq(Setting['some'])
131
+ end
132
+ end
133
+
134
+ context '#save' do
135
+ it 'normal settings' do
136
+ object = described_class.new(all_options)
137
+ object.save('base.first_setting', 'DDD')
138
+ expect(Setting['base.first_setting']).to eq('DDD')
139
+ end
140
+
141
+ it 'settings by key' do
142
+ object = described_class.new(key_options)
143
+ object.save('some.first_setting', 'LLL')
144
+ expect(Setting['some']['first_setting']).to eq('LLL')
145
+ end
146
+ end
147
+
148
+ context '#defaults' do
149
+ it 'normal defaults' do
150
+ object = described_class.new(all_options)
151
+ expect(object.defaults).to eq(RailsSettings::Default)
152
+ end
153
+
154
+ it 'old defaults' do
155
+ allow(Setting).to receive(:defaults).and_return(RailsSettings::Default.instance)
156
+ object = described_class.new(all_options)
157
+ expect(object.defaults).to be_an_instance_of(RailsSettings::Default)
158
+ end
159
+ end
160
+
161
+ context '#defaults_keys' do
162
+ it 'normal defaults keys' do
163
+ object = described_class.new(all_options)
164
+ expect(object.defaults_keys).to eq(RailsSettings::Default.instance.keys)
165
+ end
166
+ end
167
+
12
168
  def model
13
169
  subject
14
170
  end
@@ -1,176 +1,216 @@
1
- require 'spec_helper'
2
-
3
1
  RSpec.describe 'settings', type: :feature, js: true do
4
2
  before do
5
- Setting.defaults['base.first_setting'] = 'AAA'
6
- Setting.defaults['base.second_setting'] = true
7
- Setting.defaults['base.third_setting'] = 5
8
- Setting.defaults['base.four_setting'] = 5.50
9
- Setting.defaults['base.five_setting'] = :aaa
10
- Setting.defaults['second.first_setting'] = false
11
- Setting.defaults['second.second_setting'] = 'BBB'
3
+ Setting['some'] = {
4
+ 'first_setting' => 'CCC',
5
+ 'second_setting' => false
6
+ }
12
7
  Setting['base.first_setting'] = 'AAA'
13
8
  Setting['base.second_setting'] = true
14
9
  Setting['second.first_setting'] = false
15
10
  Setting['second.second_setting'] = 'BBB'
16
11
  end
17
12
 
13
+ let(:initial_some_settings) do
14
+ {
15
+ first_setting: 'CCC',
16
+ second_setting: false
17
+ }.with_indifferent_access
18
+ end
19
+
20
+ shared_examples_for 'render input with value' do |input_value|
21
+ it 'has input with value' do
22
+ expect(page).to have_selector("input[value='#{input_value}']")
23
+ end
24
+ end
25
+
26
+ shared_examples_for 'fill and save base settings to db' do
27
+ it 'saves base settings to db' do
28
+ fill_in('settings_base.first_setting', with: 'First')
29
+ uncheck('settings_base.second_setting')
30
+ fill_in('settings_base.third_setting', with: '100')
31
+ fill_in('settings_base.four_setting', with: '50.5')
32
+ fill_in('settings_base.five_setting', with: 'five')
33
+
34
+ submit
35
+
36
+ expect(Setting['base.first_setting']).to eq 'First'
37
+ expect(Setting['base.second_setting']).to eq false
38
+ expect(Setting['base.third_setting']).to eq 100
39
+ expect(Setting['base.four_setting']).to eq 50.5
40
+ expect(Setting['base.five_setting']).to eq :five
41
+ end
42
+ end
43
+
44
+ shared_examples_for 'fill and save second settings to db' do
45
+ it 'saves settings to db' do
46
+ fill_in('settings_second.second_setting', with: 'Awesome second')
47
+ check('settings_second.first_setting')
48
+
49
+ submit
50
+
51
+ expect(Setting['second.second_setting']).to eq 'Awesome second'
52
+ expect(Setting['second.first_setting']).to eq true
53
+ expect(Setting.some.with_indifferent_access).to eq(initial_some_settings)
54
+ end
55
+ end
56
+
57
+ shared_examples_for 'fill and save some settings to db' do
58
+ it 'save some settings to db' do
59
+ fill_in('settings_some.first_setting', with: 'Awesome value')
60
+ check('settings_some.second_setting')
61
+
62
+ submit
63
+
64
+ expect(Setting['some']['first_setting']).to eq 'Awesome value'
65
+ expect(Setting['some']['second_setting']).to eq true
66
+ end
67
+ end
68
+
18
69
  context 'global config' do
19
70
  before do
20
71
  ActiveadminSettingsCached.configure do |config|
21
- config.display = {'base.first_setting' => 'string', 'base.second_setting' => 'boolean',
22
- 'base.third_setting' => 'number', 'base.four_setting' => 'number',
23
- 'second.first_setting' => 'boolean', 'second.second_setting' => 'string'}
72
+ config.display = {
73
+ 'base.first_setting' => 'string',
74
+ 'base.second_setting' => 'boolean',
75
+ 'base.third_setting' => 'number',
76
+ 'base.four_setting' => 'number',
77
+ 'base.five_setting' => 'string',
78
+ 'second.first_setting' => 'boolean',
79
+ 'second.second_setting' => 'string',
80
+ 'some.first_setting' => 'string',
81
+ 'some.second_setting' => 'boolean'
82
+ }
24
83
  end
84
+
25
85
  add_setting_resource
26
86
  add_second_setting_resource
87
+ add_some_setting_resource
27
88
  add_all_setting_resource
28
89
  end
29
90
 
30
91
  context 'all setting index' do
31
- it 'when list' do
32
- visit '/admin/settings'
33
- check_base_setting
34
- check_second_setting
35
- end
92
+ before { visit '/admin/settings' }
36
93
 
37
- it 'when save' do
38
- visit '/admin/settings'
39
- check_base_setting
40
- check_second_setting
41
- fill_base_setting
42
- fill_second_setting
43
- submit
44
- fill_base_setting_check
45
- fill_second_setting_check
46
- end
94
+ it_behaves_like 'render input with value', 'AAA'
95
+ it_behaves_like 'render input with value', 'BBB'
96
+
97
+ # TODO: fixme
98
+ # it_behaves_like 'render input with value', Setting['some'].with_indifferent_access
99
+ it_behaves_like 'fill and save base settings to db'
100
+ it_behaves_like 'fill and save second settings to db'
47
101
  end
48
102
  end
49
103
 
50
104
  context 'with custom template_object' do
51
105
  context 'when right object' do
52
106
  before do
53
- template_object = ActiveadminSettingsCached::Model.new(display: {'base.first_setting' => 'string', 'base.second_setting' => 'boolean',
54
- 'base.third_setting' => 'number', 'base.four_setting' => 'number',
55
- 'second.first_setting' => 'boolean', 'second.second_setting' => 'string'})
56
- add_all_setting_resource(template_object: template_object)
57
- end
107
+ display_settings = {
108
+ 'base.first_setting' => 'string',
109
+ 'base.second_setting' => 'boolean',
110
+ 'base.third_setting' => 'number',
111
+ 'base.four_setting' => 'number',
112
+ 'base.five_setting' => 'string',
113
+ 'second.first_setting' => 'boolean',
114
+ 'second.second_setting' => 'string'
115
+ }
116
+
117
+ add_all_setting_resource(
118
+ template_object: ActiveadminSettingsCached::Model.new(display: display_settings)
119
+ )
58
120
 
59
- it do
60
121
  visit '/admin/base_settings'
61
- check_base_setting
62
122
  end
123
+
124
+ it_behaves_like 'render input with value', 'AAA'
63
125
  end
64
126
 
65
127
  context 'when wrong object' do
66
128
  before do
67
- template_object = nil
68
- add_all_setting_resource(template_object: template_object)
69
- end
129
+ add_all_setting_resource(template_object: nil)
70
130
 
71
- it do
72
131
  visit '/admin/base_settings'
73
- check_base_setting
74
132
  end
133
+
134
+ it_behaves_like 'render input with value', 'AAA'
75
135
  end
76
136
  end
77
137
 
78
- context 'per page' do
138
+ context 'when settings on different pages' do
79
139
  before do
80
140
  ActiveadminSettingsCached.configure do |config|
81
141
  config.display = {}
82
142
  end
83
- add_setting_resource(display: {'base.first_setting' => 'string', 'base.second_setting' => 'boolean',
84
- 'base.third_setting' => 'number', 'base.four_setting' => 'number'})
85
- add_second_setting_resource(display: {'second.first_setting' => 'boolean', 'second.second_setting' => 'string'})
86
- add_all_setting_resource(display: {'base.first_setting' => 'string', 'base.second_setting' => 'boolean',
87
- 'base.third_setting' => 'number', 'base.four_setting' => 'number',
88
- 'second.first_setting' => 'boolean', 'second.second_setting' => 'string'})
89
- end
90
-
91
- context 'setting index' do
92
- it 'when list' do
93
- visit '/admin/base_settings'
94
- check_base_setting
95
- end
96
143
 
97
- it 'when save' do
98
- visit '/admin/base_settings'
99
- check_base_setting
100
- fill_base_setting
101
- submit
102
- fill_base_setting_check
103
- end
144
+ add_setting_resource(
145
+ display: {
146
+ 'base.first_setting' => 'string',
147
+ 'base.second_setting' => 'boolean',
148
+ 'base.third_setting' => 'number',
149
+ 'base.four_setting' => 'number',
150
+ 'base.five_setting' => 'string'
151
+ }
152
+ )
153
+
154
+ add_second_setting_resource(
155
+ display: {
156
+ 'second.first_setting' => 'boolean',
157
+ 'second.second_setting' => 'string'
158
+ }
159
+ )
160
+
161
+ add_some_setting_resource(
162
+ display: {
163
+ 'some.first_setting' => 'string',
164
+ 'some.second_setting' => 'boolean'
165
+ }
166
+ )
167
+
168
+ add_all_setting_resource(
169
+ display: {
170
+ 'base.first_setting' => 'string',
171
+ 'base.second_setting' => 'boolean',
172
+ 'base.third_setting' => 'number',
173
+ 'base.four_setting' => 'number',
174
+ 'base.five_setting' => 'string',
175
+ 'second.first_setting' => 'boolean',
176
+ 'second.second_setting' => 'string'
177
+ }
178
+ )
104
179
  end
105
180
 
106
- context 'second setting index' do
107
- it 'when list' do
108
- visit '/admin/second_settings'
109
- check_second_setting
110
- end
181
+ context 'base settings page' do
182
+ before { visit '/admin/base_settings' }
111
183
 
112
- it 'when save' do
113
- visit '/admin/second_settings'
114
- check_second_setting
115
- fill_second_setting
116
- submit
117
- fill_second_setting_check
118
- end
184
+ it_behaves_like 'render input with value', 'AAA'
185
+ it_behaves_like 'fill and save base settings to db'
119
186
  end
120
187
 
121
- context 'all setting index' do
122
- it 'when list' do
123
- visit '/admin/settings'
124
- check_base_setting
125
- check_second_setting
126
- end
188
+ context 'second setting page' do
189
+ before { visit '/admin/second_settings' }
127
190
 
128
- it 'when save' do
129
- visit '/admin/settings'
130
- check_base_setting
131
- check_second_setting
132
- fill_base_setting
133
- fill_second_setting
134
- submit
135
- fill_base_setting_check
136
- fill_second_setting_check
137
- end
191
+ it_behaves_like 'render input with value', 'BBB'
192
+ it_behaves_like 'fill and save second settings to db'
138
193
  end
139
- end
140
-
141
- def check_base_setting
142
- expect(page).to have_selector("input[value='#{Setting['base.first_setting']}']")
143
- end
144
-
145
- def check_second_setting
146
- expect(page).to have_selector("input[value='#{Setting['second.second_setting']}']")
147
- end
148
194
 
149
- def fill_base_setting
150
- fill_in('settings_base.first_setting', with: 'BBB')
151
- uncheck('settings_base.second_setting')
152
- fill_in('settings_base.third_setting', with: 100)
153
- fill_in('settings_base.four_setting', with: 150.55)
154
- fill_in('settings_base.five_setting', with: 'bbb')
195
+ context 'some setting index' do
196
+ before { visit '/admin/some_settings' }
155
197
 
156
- end
198
+ it_behaves_like 'render input with value', 'CCC'
199
+ it_behaves_like 'fill and save some settings to db'
200
+ end
157
201
 
158
- def fill_second_setting
159
- fill_in('settings_second.second_setting', with: 'AAA')
160
- check('settings_second.first_setting')
161
- end
202
+ context 'all setting index' do
203
+ before { visit '/admin/settings' }
162
204
 
163
- def fill_base_setting_check
164
- expect(Setting['base.first_setting']).to eq 'BBB'
165
- expect(Setting['base.second_setting']).to eq false
166
- expect(Setting['base.third_setting']).to eq 100
167
- expect(Setting['base.four_setting']).to eq 150.55
168
- expect(Setting['base.five_setting']).to eq :bbb
169
- end
205
+ it_behaves_like 'render input with value', 'AAA'
206
+ it_behaves_like 'render input with value', 'BBB'
207
+ # FIXME
208
+ #it_behaves_like 'render input with value', Setting['some'].with_indifferent_access
170
209
 
171
- def fill_second_setting_check
172
- expect(Setting['second.second_setting']).to eq 'AAA'
173
- expect(Setting['second.first_setting']).to eq true
210
+ it_behaves_like 'fill and save base settings to db'
211
+ it_behaves_like 'fill and save second settings to db'
212
+ it { expect(Setting.some.with_indifferent_access).to eq(initial_some_settings) }
213
+ end
174
214
  end
175
215
 
176
216
  def submit
@@ -16,6 +16,15 @@ def add_second_setting_resource(options = {}, &block)
16
16
  Rails.application.reload_routes!
17
17
  end
18
18
 
19
+ def add_some_setting_resource(options = {}, &block)
20
+ options.merge!({model_name: 'Setting', title: 'Some Settings', key: 'some'})
21
+ ActiveAdmin.register_page options[:title] do
22
+ menu label: options[:title], priority: 99, parent: 'settings'
23
+ active_admin_settings_page(options, &block)
24
+ end
25
+ Rails.application.reload_routes!
26
+ end
27
+
19
28
  def add_all_setting_resource(options = {}, &block)
20
29
  options.merge!({model_name: 'Setting', title: 'Settings'})
21
30
  ActiveAdmin.register_page options[:title] do
@@ -4,8 +4,9 @@ generate :settings, 'Setting'
4
4
 
5
5
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
6
 
7
- generate :'active_admin:install --skip-users'
7
+ generate :'active_admin:install --skip --skip-users'
8
8
  generate :'formtastic:install'
9
+ generate :'settings:install'
9
10
 
10
11
  # Configure Setup
11
12
  inject_into_file 'config/initializers/active_admin.rb', <<-RUBY, before: "ActiveAdmin.setup do |config|"
@@ -14,6 +15,21 @@ inject_into_file 'config/initializers/active_admin.rb', <<-RUBY, before: "Active
14
15
  end
15
16
  RUBY
16
17
 
18
+ # Default Settings
19
+ inject_into_file 'config/app.yml', <<-YAML, after: "defaults: &defaults\n"
20
+ some:
21
+ first_setting: AAA
22
+ second_setting: true
23
+ 'base.first_setting': AAA
24
+ 'base.second_setting': true
25
+ 'base.third_setting': 5
26
+ 'base.four_setting': 5.50
27
+ 'base.five_setting': :aaa
28
+ 'base.six_setting': ['a', 'b']
29
+ 'second.first_setting': false
30
+ 'second.second_setting': BBB
31
+ YAML
32
+
17
33
  route "root :to => 'admin/dashboard#index'"
18
34
 
19
35
  rake 'db:migrate'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activeadmin_settings_cached
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Semyon Pupkov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-25 00:00:00.000000000 Z
11
+ date: 2016-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activeadmin
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: 0.5.3
34
34
  - - "<"
35
35
  - !ruby/object:Gem::Version
36
- version: 0.5.5
36
+ version: 0.6.6
37
37
  type: :runtime
38
38
  prerelease: false
39
39
  version_requirements: !ruby/object:Gem::Requirement
@@ -43,7 +43,21 @@ dependencies:
43
43
  version: 0.5.3
44
44
  - - "<"
45
45
  - !ruby/object:Gem::Version
46
- version: 0.5.5
46
+ version: 0.6.6
47
+ - !ruby/object:Gem::Dependency
48
+ name: dry-types
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 0.8.1
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 0.8.1
47
61
  - !ruby/object:Gem::Dependency
48
62
  name: bundler
49
63
  requirement: !ruby/object:Gem::Requirement
@@ -86,6 +100,20 @@ dependencies:
86
100
  - - ">="
87
101
  - !ruby/object:Gem::Version
88
102
  version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: appraisal
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
89
117
  - !ruby/object:Gem::Dependency
90
118
  name: database_cleaner
91
119
  requirement: !ruby/object:Gem::Requirement
@@ -178,7 +206,10 @@ extensions: []
178
206
  extra_rdoc_files: []
179
207
  files:
180
208
  - ".gitignore"
209
+ - ".rspec"
181
210
  - ".travis.yml"
211
+ - Appraisals
212
+ - Dockerfile
182
213
  - Gemfile
183
214
  - LICENSE.txt
184
215
  - Makefile
@@ -186,8 +217,10 @@ files:
186
217
  - Rakefile
187
218
  - activeadmin_settings_cached.gemspec
188
219
  - app/views/admin/settings/_index.html.erb
220
+ - config/locales/de.yml
189
221
  - config/locales/en.yml
190
222
  - config/locales/ru.yml
223
+ - docker-compose.yml
191
224
  - lib/activeadmin_settings_cached.rb
192
225
  - lib/activeadmin_settings_cached/coercions.rb
193
226
  - lib/activeadmin_settings_cached/dsl.rb
@@ -225,7 +258,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
225
258
  version: '0'
226
259
  requirements: []
227
260
  rubyforge_project:
228
- rubygems_version: 2.4.5
261
+ rubygems_version: 2.5.1
229
262
  signing_key:
230
263
  specification_version: 4
231
264
  summary: UI interface for rails-settings-cached in active admin