rails_admin_settings 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.3-p392@rails_admin_settings --create
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ services: mongodb
3
+ rvm:
4
+ - 1.9.3
5
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails_admin_settings.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Gleb Tv
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # RailsAdminSettings
2
+
3
+ [![Build Status](https://secure.travis-ci.org/rs-pro/rails_admin_settings.png?branch=master)](http://travis-ci.org/rs-pro/rails_admin_settings)
4
+ [![Dependency Status](https://gemnasium.com/rs-pro/rails_admin_settings.png)](https://gemnasium.com/rs-pro/rails_admin_settings)
5
+
6
+ Note: This gem currently supports Mongoid only, as I don't need AR support.
7
+
8
+ Pull request with AR support will be welcome
9
+
10
+ ## Features
11
+
12
+ - Lazy loading - loads settings only if they are needed during request
13
+ - Loads all settings at once and caches them for the duration of request
14
+ - Supports lots of setting types - yaml, html with ckeditor, phone numbers etc
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ gem 'rails_admin_settings'
21
+
22
+ ## Gemfile order matters
23
+
24
+ - Put it after rails_admin to get built-in support
25
+ - Put it after rails_admin_toggleable to get built-in support
26
+ - Put it after ckeditor/glebtv-ckeditor/rich to get built-in support
27
+ - Put it after russian_phone to get built-in support
28
+ - Put it after sanitized to get built-in support
29
+ - Put it after safe_yaml to get built-in support
30
+
31
+ And then execute:
32
+
33
+ $ bundle
34
+
35
+ Or install it yourself as:
36
+
37
+ $ gem install rails_admin_settings
38
+
39
+ ## Basic Usage (like RailsSettings)
40
+
41
+ Settings.admin_email = 'test@example.com'
42
+ Settings.admin_email
43
+
44
+
45
+ ## Advanced Usage
46
+
47
+ Settings.content_block_1(mode: 'html', default: 'test')
48
+ Settings.data(mode: 'yaml')
49
+ Settings.data = [1, 2, 3]
50
+
51
+ See more here: https://github.com/rs-pro/rails_admin_settings/blob/master/spec/advanced_usage_spec.rb
52
+
53
+ ## Value types
54
+
55
+ Supported types:
56
+
57
+ string (input)
58
+ text (textarea)
59
+ html (supports Rich, glebtv-ckeditor, ckeditor, but does not require any of them)
60
+ integer (stored as string)
61
+ yaml (requires safe_yaml)
62
+ phone (requires russian_phone)
63
+ sanitized (requires sanitize)
64
+
65
+ Strings and html support following replacement patterns:
66
+
67
+ {{year}} -> current year
68
+ {{year|2013}} -> 2013 in 2013, 2013-2014 in 2014, etc
69
+
70
+ ## Usage with Rails Admin
71
+
72
+ Rails admin management for settings is supported out of the box
73
+
74
+ ## Contributing
75
+
76
+ 1. Fork it
77
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
78
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
79
+ 4. Push to the branch (`git push origin my-new-feature`)
80
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc 'Default: run specs.'
6
+ task :default => :spec
7
+
8
+ desc "Run specs"
9
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,20 @@
1
+ - if @object.type == 'string' || @object.type == 'integer'
2
+ = form.text_field :raw, :value => field.value
3
+ - elsif @object.type == 'text' || @object.type == 'yaml'
4
+ = form.text_area :raw, :value => field.value, :rows => 10, :cols => 80
5
+ - elsif @object.type == 'html'
6
+ - if form.respond_to?(:cktext_area)
7
+ = javascript_include_tag "ckeditor/init"
8
+ = form.cktext_area :raw, :value => field.value, :rows => 10, :cols => 80
9
+ - elsif Object.const_defined?('Rich')
10
+ = form.text_area :raw, :value => field.value, :rows => 10, :cols => 80
11
+
12
+ = javascript_include_tag "rich/base.js"
13
+ javascript:
14
+ var instance = CKEDITOR.instances['#{form.dom_id(field)}'];
15
+ if(instance) { CKEDITOR.remove(instance); }
16
+ CKEDITOR.replace('#{form.dom_id(field)}', #{Rich.options().to_json.html_safe});
17
+ - else
18
+ .alert.alert-error
19
+ = t('admin.settings.no_ckeditor_detected')
20
+ = form.text_area :raw, :value => field.value, :rows => 10, :cols => 80
@@ -0,0 +1,4 @@
1
+ ru:
2
+ admin:
3
+ settings:
4
+ label: 'Settings'
@@ -0,0 +1,17 @@
1
+ ru:
2
+ admin:
3
+ settings:
4
+ label: 'Настройки'
5
+ no_ckeditor_detected: CKEditor не обнаружен — отображаю настройку как текстовое поле
6
+ phone_invalid: Неверный телефонный номер
7
+ yaml_invalid: Неверный синтаксис YAML
8
+ mongoid:
9
+ models:
10
+ rails_admin_settings/setting: Настройки
11
+
12
+ attributes:
13
+ rails_admin_settings/setting:
14
+ key: Название
15
+ raw: Значение
16
+ type: Тип
17
+
@@ -0,0 +1,11 @@
1
+ module RailsAdminSettings
2
+ class Engine < ::Rails::Engine
3
+ initializer 'RailsAdminSettings Install after_filter' do |app|
4
+ if defined?(ActionController) and defined?(ActionController::Base)
5
+ ActionController::Base.class_eval do
6
+ after_filter { Settings.unload! }
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,118 @@
1
+ module RailsAdminSettings
2
+ module Processing
3
+ def text_type?
4
+ ['string', 'html', 'sanitized'].include? type
5
+ end
6
+
7
+ def html_type?
8
+ ['html', 'sanitized'].include? type
9
+ end
10
+
11
+ def integer_type?
12
+ 'integer' == type
13
+ end
14
+
15
+ def yaml_type?
16
+ 'yaml' == type
17
+ end
18
+
19
+ def phone_type?
20
+ 'phone' == type
21
+ end
22
+
23
+ def sanitized_type?
24
+ 'sanitized' == type
25
+ end
26
+
27
+ def value
28
+ if raw.blank? || disabled?
29
+ default_value
30
+ else
31
+ processed_value
32
+ end
33
+ end
34
+
35
+ def to_s
36
+ if yaml_type? || phone_type? || integer_type?
37
+ raw
38
+ else
39
+ value
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def sanitize_value
46
+ require_sanitize do
47
+ self.raw = Sanitize.clean(value, Sanitize::Config::RELAXED)
48
+ end
49
+ end
50
+
51
+ def default_value
52
+ if html_type?
53
+ ''.html_safe
54
+ elsif text_type?
55
+ ''
56
+ elsif integer_type?
57
+ 0
58
+ elsif yaml_type?
59
+ nil
60
+ elsif phone_type?
61
+ require_russian_phone do
62
+ RussianPhone::Number.new('(000) 000-00-00')
63
+ end
64
+ else
65
+ nil
66
+ end
67
+ end
68
+
69
+ def process_text
70
+ replacer = Proc.new do |m|
71
+ if Time.now.strftime('%Y') == $1
72
+ $1
73
+ else
74
+ "#{$1}-#{Time.now.strftime('%Y')}"
75
+ end
76
+ end
77
+
78
+ text = raw.dup
79
+
80
+ text.gsub!('{{year}}', Time.now.strftime('%Y'))
81
+ text.gsub!(/\{\{year\|([\d]{4})\}\}/, &replacer)
82
+ text = text.html_safe if html_type?
83
+ text
84
+ end
85
+
86
+ def load_phone
87
+ require_russian_phone do
88
+ RussianPhone::Number.new(raw)
89
+ end
90
+ end
91
+
92
+ def load_yaml
93
+ require_safe_yaml do
94
+ YAML.safe_load(raw)
95
+ end
96
+ end
97
+
98
+ def processed_value
99
+ if text_type?
100
+ process_text
101
+ elsif integer_type?
102
+ raw.to_i
103
+ elsif yaml_type?
104
+ load_yaml
105
+ elsif phone_type?
106
+ load_phone
107
+ else
108
+ nil
109
+ end
110
+ end
111
+
112
+ def self.included(base)
113
+ base.class_eval do
114
+ alias_method :val, :value
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,39 @@
1
+ module RailsAdminSettings
2
+ module RailsAdminConfig
3
+ def self.included(base)
4
+ rails_admin do
5
+ navigation_label t('admin.settings.label')
6
+
7
+ object_label_method do
8
+ :key
9
+ end
10
+
11
+ list do
12
+ if Object.const_defined?('RailsAdminToggleable')
13
+ field :enabled, :toggle
14
+ else
15
+ field :enabled
16
+ end
17
+
18
+ field :key
19
+ field :type
20
+ end
21
+
22
+ edit do
23
+ field :enabled
24
+
25
+ field :raw do
26
+ partial "setting_value"
27
+ end
28
+
29
+ field :key do
30
+ read_only true
31
+ end
32
+ field :type do
33
+ read_only true
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,36 @@
1
+ module RailsAdminSettings
2
+ module RequireHelpers
3
+ private
4
+
5
+ def require_russian_phone
6
+ begin
7
+ require 'russian_phone'
8
+ yield
9
+ rescue LoadError => e
10
+ e.message << " [rails_admin_settings] Please add gem 'russian_phone' to use phone settings"
11
+ raise e
12
+ end
13
+ end
14
+
15
+ def require_safe_yaml
16
+ begin
17
+ require 'safe_yaml'
18
+ yield
19
+ rescue LoadError => e
20
+ e.message << " [rails_admin_settings] Please add gem 'safe_yaml' to your Gemfile to use yaml settings"
21
+ raise e
22
+ end
23
+ end
24
+
25
+ def require_sanitize
26
+ begin
27
+ require 'sanitize'
28
+ yield
29
+ rescue LoadError => e
30
+ e.message << " [rails_admin_settings] Please install sanitize to use sanitized settings"
31
+ raise e
32
+ end
33
+ end
34
+ end
35
+ end
36
+
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ module RailsAdminSettings
3
+ class Setting
4
+ include ::Mongoid::Document
5
+ include ::Mongoid::Timestamps::Short
6
+
7
+ if Object.const_defined?('Mongoid') && Mongoid.const_defined?('Audit')
8
+ include ::Mongoid::Audit::Trackable
9
+ track_history track_create: true, track_destroy: true
10
+ end
11
+
12
+ field :enabled, type: Boolean, default: true
13
+ scope :enabled, where(enabled: true)
14
+
15
+ field :type, type: String, default: RailsAdminSettings.types.first
16
+
17
+ field :key, type: String
18
+ field :raw, type: String, default: ''
19
+
20
+ include RailsAdminSettings::RequireHelpers
21
+ include RailsAdminSettings::Processing
22
+ include RailsAdminSettings::Validation
23
+
24
+ def disabled?
25
+ !enabled
26
+ end
27
+
28
+ def enabled?
29
+ enabled
30
+ end
31
+
32
+ index(key: 1)
33
+
34
+ if respond_to?(:rails_admin)
35
+ include RailsAdminSettings::RailsAdminConfig
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,95 @@
1
+ class Settings
2
+ class << self
3
+ def load!
4
+ unless @@loaded
5
+ @@settings = {}
6
+ @@loaded = true
7
+ end
8
+ end
9
+
10
+ def unload!
11
+ @@settings = {}
12
+ @@loaded = false
13
+ end
14
+
15
+ # returns processed setting value
16
+ def method_missing(key, *args)
17
+ if key[-1] == '='
18
+ key = key[0..-2]
19
+ options = args[1] || {}
20
+ value = args.first
21
+ set(key, value, options).val
22
+ else
23
+ get(key, args.first || {}).val
24
+ end
25
+ end
26
+
27
+ def set(key, value = nil, options = {})
28
+ load!
29
+ key = key.to_s
30
+ options.symbolize_keys!
31
+
32
+
33
+ if !options[:type].nil? && options[:type] == 'yaml' && !value.nil? && !valid_yaml?(value)
34
+ value = value.to_yaml
35
+ end
36
+
37
+ if @@settings[key].nil?
38
+ @@settings[key] = RailsAdminSettings::Setting.create(options.merge(key: key, raw: value))
39
+ else
40
+ @@settings[key].update_attributes!(options.merge(raw: value))
41
+ @@settings[key]
42
+ end
43
+ end
44
+
45
+ def valid_yaml?(value)
46
+ begin
47
+ YAML.safe_load(value)
48
+ rescue LoadError => e
49
+ e.message << " [rails_admin_settings] Please add gem 'safe_yaml' to your Gemfile to use yaml settings"
50
+ raise e
51
+ rescue Psych::SyntaxError => e
52
+ return false
53
+ end
54
+ end
55
+
56
+ # returns setting object
57
+ def get(key, options = {})
58
+ load!
59
+ key = key.to_s
60
+ if @@settings[key].nil?
61
+ create_setting(key, options)
62
+ else
63
+ @@settings[key]
64
+ end
65
+ end
66
+
67
+ def []=(key, value)
68
+ set(key, value)
69
+ end
70
+
71
+ def [](key)
72
+ get(key)
73
+ end
74
+
75
+ def save_default(key, value, options = {})
76
+ load!
77
+ options.merge!(default: value)
78
+ create_setting(key, options) if @@settings[key].nil?
79
+ end
80
+
81
+ def create_setting(key, options = {})
82
+ load!
83
+ options.symbolize_keys!
84
+ options[:raw] = options.delete(:default)
85
+ @@settings[key] = RailsAdminSettings::Setting.create(options.merge(key: key)) if @@settings[key].nil?
86
+ end
87
+
88
+ # to satisfy rspec
89
+ def to_ary
90
+ ['Settings']
91
+ end
92
+ end
93
+
94
+ self.unload!
95
+ end
@@ -0,0 +1,5 @@
1
+ module RailsAdminSettings
2
+ def self.types
3
+ ['string', 'integer', 'html', 'sanitized', 'yaml', 'phone']
4
+ end
5
+ end
@@ -0,0 +1,30 @@
1
+ module RailsAdminSettings
2
+ module Validation
3
+ def self.included(base)
4
+ base.before_validation do
5
+ self.raw = default_value if raw.blank?
6
+ end
7
+ base.before_validation :sanitize_value, if: :sanitized_type?
8
+ base.validates_uniqueness_of :key
9
+ base.validates_inclusion_of :type, in: RailsAdminSettings.types
10
+ base.validates_numericality_of :raw, if: :integer_type?
11
+
12
+ base.validate if: :phone_type? do
13
+ require_russian_phone do
14
+ errors.add(:raw, t('admin.settings.phone_invalid')) if RussianPhone::Number.new(:raw).valid?
15
+ end
16
+ end
17
+ base.validate if: :yaml_type? do
18
+ require_safe_yaml do
19
+ unless raw.blank?
20
+ begin
21
+ YAML.safe_load(raw)
22
+ rescue Psych::SyntaxError => e
23
+ errors.add(:raw, t('admin.settings.yaml_invalid'))
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module RailsAdminSettings
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,16 @@
1
+ require "rails_admin_settings/version"
2
+
3
+ module RailsAdminSettings
4
+ autoload :Processing, "rails_admin_settings/processing"
5
+ autoload :Validation, "rails_admin_settings/validation"
6
+ autoload :RequireHelpers, "rails_admin_settings/require_helpers"
7
+ autoload :RailsAdminConfig, "rails_admin_settings/rails_admin_config"
8
+ autoload :Setting, "rails_admin_settings/setting"
9
+ end
10
+
11
+ require "rails_admin_settings/types"
12
+ require "rails_admin_settings/settings"
13
+
14
+ if Object.const_defined?('Rails')
15
+ require "rails_admin_settings/engine"
16
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails_admin_settings/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails_admin_settings"
8
+ spec.version = RailsAdminSettings::VERSION
9
+ spec.authors = ["Gleb Tv"]
10
+ spec.email = ["glebtv@gmail.com"]
11
+ spec.description = %q{Mongoid / RailsAdmin App Settings management}
12
+ spec.summary = %q{Setting for Rails app with mongoid and RailsAdmin}
13
+ spec.homepage = "https://github.com/rs-pro/rails_admin_settings"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "mongoid", '~> 3.1'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3.4"
24
+ spec.add_development_dependency "rake", "~> 10.0.3"
25
+ spec.add_development_dependency "rspec", "~> 2.13.0"
26
+ spec.add_development_dependency "mongoid-rspec", "~> 1.7.0"
27
+ spec.add_development_dependency "simplecov", "~> 0.7.1"
28
+ spec.add_development_dependency "database_cleaner", "~> 0.9.1"
29
+ spec.add_development_dependency "factory_girl", "~> 4.2.0"
30
+
31
+ spec.add_development_dependency "safe_yaml", "~> 0.8.6"
32
+ spec.add_development_dependency "russian_phone", "~> 0.3.2"
33
+ spec.add_development_dependency "sanitize", "~> 2.0.3"
34
+ end
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'Settings advanced usage' do
6
+ it 'with defaults' do
7
+ s = Settings.email(default: 'test@example.com')
8
+ s.should eq 'test@example.com'
9
+ Settings.get(:email).to_s.should eq 'test@example.com'
10
+ end
11
+
12
+ it 'support html mode' do
13
+ Settings.get(:email, type: 'html').to_s.should eq 'test@example.com'
14
+ end
15
+
16
+ it 'support integer mode' do
17
+ Settings.get(:testint, type: 'integer').value.should eq 0
18
+ Settings.get(:testint, default: 5, type: 'integer').value.should eq 0
19
+ Settings.get(:testint2, default: 5, type: 'integer').value.should eq 5
20
+ Settings.testint2.should eq 5
21
+ end
22
+
23
+ it 'support yaml mode' do
24
+ Settings.get(:test_yml, type: 'yaml').value.should be_nil
25
+ Settings.get(:test_yml_2, default: '[t1, t2, t3]', type: 'yaml').value.should eq ['t1', 't2', 't3']
26
+ Settings.test_yml_2.should eq ['t1', 't2', 't3']
27
+ end
28
+
29
+ it 'should have sensible defaults' do
30
+ s = Settings.get(:test_sett)
31
+ s.should_not be_nil
32
+ s.type.should eq 'string'
33
+ s.raw.should eq ''
34
+ s.value.should eq ''
35
+ end
36
+
37
+ it 'should support yaml type' do
38
+ Settings.set(:data, '[one, two, three]', type: 'yaml')
39
+ Settings.get(:data).raw.should eq '[one, two, three]'
40
+ Settings.data.should eq ['one', 'two', 'three']
41
+ end
42
+
43
+ it 'should support phone type' do
44
+ Settings.set(:tphone, '906 111 11 11', type: 'phone')
45
+ Settings.get(:tphone).val.class.name.should eq 'RussianPhone::Number'
46
+ Settings.tphone.class.name.should eq 'RussianPhone::Number'
47
+ Settings.tphone.should eq '906 111 11 11'
48
+
49
+ Settings.get(:tphone).val.city.should eq '906'
50
+ Settings.get(:tphone).val.formatted_subscriber.should eq '111-11-11'
51
+
52
+ Settings.dphone(type: 'phone')
53
+
54
+ Settings.dphone.city.should eq '000'
55
+ Settings.dphone.formatted_subscriber.should eq '000-00-00'
56
+ end
57
+ end
@@ -0,0 +1,8 @@
1
+ # coding: utf-8
2
+
3
+ FactoryGirl.define do
4
+ factory :setting, class: RailsAdminSettings::Setting do
5
+ sequence(:key){|n| "setting_#{n}" }
6
+ raw "Контент 1"
7
+ end
8
+ end
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe RailsAdminSettings::Setting do
6
+ it { should have_fields(:enabled, :key, :type, :raw) }
7
+
8
+ it "correctly return content when enabled" do
9
+ setting = FactoryGirl.create(:setting)
10
+ setting.to_s.should eq "Контент 1"
11
+ end
12
+
13
+ it "return empty string when disabled" do
14
+ setting = FactoryGirl.create(:setting, enabled: false)
15
+ setting.to_s.should eq ""
16
+ end
17
+
18
+ it "correctly process {{year}}" do
19
+ setting = FactoryGirl.create(:setting, raw: '&copy; {{year}} company')
20
+ setting.val.should eq "&copy; #{Time.now.strftime('%Y')} company"
21
+ end
22
+
23
+ it "correctly process {{year|2010}}" do
24
+ setting = FactoryGirl.create(:setting, raw: '&copy; {{year|2010}} company')
25
+ setting.val.should eq "&copy; 2010-#{Time.now.strftime('%Y')} company"
26
+ end
27
+
28
+ it "correctly process {{year|current_year}}" do
29
+ setting = FactoryGirl.create(:setting, raw: '&copy; {{year|' + Time.now.strftime('%Y') + '}} company')
30
+ setting.val.should eq "&copy; #{Time.now.strftime('%Y')} company"
31
+ setting.val.class.name.should_not eq "ActiveSupport::SafeBuffer"
32
+ end
33
+
34
+ it 'return html_safe string when in html mode' do
35
+ setting = FactoryGirl.create(:setting, raw: '&copy; {{year}} company', type: 'html')
36
+ setting.val.should eq "&copy; #{Time.now.strftime('%Y')} company"
37
+ setting.val.class.name.should eq "ActiveSupport::SafeBuffer"
38
+ end
39
+
40
+ it 'sanitize html when in sanitized mode' do
41
+ setting = FactoryGirl.create(:setting, raw: '&copy; {{year}} company <a href="javascript:alert()">test</a>', type: 'sanitized')
42
+ setting.val.should eq "© #{Time.now.strftime('%Y')} company <a>test</a>"
43
+ setting.val.class.name.should eq "ActiveSupport::SafeBuffer"
44
+ end
45
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Settings do
6
+
7
+ it "should works as RailsSettings" do
8
+ email = "my@mail.ru"
9
+ Settings.email = email
10
+ Settings.email.should == email
11
+ end
12
+
13
+ it "should save default" do
14
+ email = "my@mail.ru"
15
+ email2 = "my2@mail.ru"
16
+ Settings.save_default(:email, email)
17
+ Settings.email.should == email
18
+ Settings.email = email2
19
+ Settings.email.should == email2
20
+ Settings.save_default(:email, email)
21
+ Settings.email.should == email2
22
+ end
23
+
24
+ it 'should properly unload' do
25
+ Settings.class_eval { cattr_accessor :loaded }
26
+ Settings.load!
27
+ Settings.loaded.should eq true
28
+ Settings.unload!
29
+ Settings.loaded.should eq false
30
+ end
31
+
32
+ it 'should support yaml type' do
33
+ Settings.tdata(type: 'yaml')
34
+ Settings.tdata = ['one', 'two', 'three']
35
+ YAML.safe_load(Settings.get(:tdata).raw).should eq ['one', 'two', 'three']
36
+ Settings.tdata.should eq ['one', 'two', 'three']
37
+ end
38
+ end
@@ -0,0 +1,23 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'rubygems'
4
+
5
+ require 'simplecov'
6
+ SimpleCov.start
7
+
8
+ require 'bundler/setup'
9
+ require 'active_support/core_ext'
10
+ require 'mongoid'
11
+ require 'database_cleaner'
12
+ require 'factory_girl'
13
+ require 'mongoid-rspec'
14
+
15
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each do |f|
16
+ require f
17
+ end
18
+
19
+ require 'rails_admin_settings'
20
+
21
+ Dir["#{File.dirname(__FILE__)}/factories/**/*.rb"].each do |f|
22
+ require f
23
+ end
@@ -0,0 +1,9 @@
1
+ RSpec.configure do |config|
2
+ config.before :suite do
3
+ DatabaseCleaner.strategy = :truncation
4
+ end
5
+ config.after :each do
6
+ DatabaseCleaner.clean
7
+ end
8
+ end
9
+
@@ -0,0 +1,6 @@
1
+ ENV["MONGOID_ENV"] = "test"
2
+ Mongoid.load!("spec/support/mongoid.yml")
3
+
4
+ RSpec.configure do |configuration|
5
+ configuration.include Mongoid::Matchers
6
+ end
@@ -0,0 +1,8 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ database: mongoid-history
5
+ hosts:
6
+ - localhost:27017
7
+ options:
8
+ consistency: :strong
metadata ADDED
@@ -0,0 +1,266 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_admin_settings
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gleb Tv
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
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: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.3.4
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.3.4
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 10.0.3
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 10.0.3
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 2.13.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 2.13.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: mongoid-rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 1.7.0
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 1.7.0
94
+ - !ruby/object:Gem::Dependency
95
+ name: simplecov
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 0.7.1
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 0.7.1
110
+ - !ruby/object:Gem::Dependency
111
+ name: database_cleaner
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 0.9.1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: 0.9.1
126
+ - !ruby/object:Gem::Dependency
127
+ name: factory_girl
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ~>
132
+ - !ruby/object:Gem::Version
133
+ version: 4.2.0
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: 4.2.0
142
+ - !ruby/object:Gem::Dependency
143
+ name: safe_yaml
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ~>
148
+ - !ruby/object:Gem::Version
149
+ version: 0.8.6
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ~>
156
+ - !ruby/object:Gem::Version
157
+ version: 0.8.6
158
+ - !ruby/object:Gem::Dependency
159
+ name: russian_phone
160
+ requirement: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ~>
164
+ - !ruby/object:Gem::Version
165
+ version: 0.3.2
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ~>
172
+ - !ruby/object:Gem::Version
173
+ version: 0.3.2
174
+ - !ruby/object:Gem::Dependency
175
+ name: sanitize
176
+ requirement: !ruby/object:Gem::Requirement
177
+ none: false
178
+ requirements:
179
+ - - ~>
180
+ - !ruby/object:Gem::Version
181
+ version: 2.0.3
182
+ type: :development
183
+ prerelease: false
184
+ version_requirements: !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - ~>
188
+ - !ruby/object:Gem::Version
189
+ version: 2.0.3
190
+ description: Mongoid / RailsAdmin App Settings management
191
+ email:
192
+ - glebtv@gmail.com
193
+ executables: []
194
+ extensions: []
195
+ extra_rdoc_files: []
196
+ files:
197
+ - .gitignore
198
+ - .rspec
199
+ - .rvmrc
200
+ - .travis.yml
201
+ - Gemfile
202
+ - LICENSE.txt
203
+ - README.md
204
+ - Rakefile
205
+ - app/views/rails_admin/main/_setting_value.html.haml
206
+ - config/locales/en.rails_admin_settings.yml
207
+ - config/locales/ru.rails_admin_settings.yml
208
+ - lib/rails_admin_settings.rb
209
+ - lib/rails_admin_settings/engine.rb
210
+ - lib/rails_admin_settings/processing.rb
211
+ - lib/rails_admin_settings/rails_admin_config.rb
212
+ - lib/rails_admin_settings/require_helpers.rb
213
+ - lib/rails_admin_settings/setting.rb
214
+ - lib/rails_admin_settings/settings.rb
215
+ - lib/rails_admin_settings/types.rb
216
+ - lib/rails_admin_settings/validation.rb
217
+ - lib/rails_admin_settings/version.rb
218
+ - rails_admin_settings.gemspec
219
+ - spec/advanced_usage_spec.rb
220
+ - spec/factories/setting.rb
221
+ - spec/model_spec.rb
222
+ - spec/settings_spec.rb
223
+ - spec/spec_helper.rb
224
+ - spec/support/database_cleaner.rb
225
+ - spec/support/mongoid.rb
226
+ - spec/support/mongoid.yml
227
+ homepage: https://github.com/rs-pro/rails_admin_settings
228
+ licenses:
229
+ - MIT
230
+ post_install_message:
231
+ rdoc_options: []
232
+ require_paths:
233
+ - lib
234
+ required_ruby_version: !ruby/object:Gem::Requirement
235
+ none: false
236
+ requirements:
237
+ - - ! '>='
238
+ - !ruby/object:Gem::Version
239
+ version: '0'
240
+ segments:
241
+ - 0
242
+ hash: -2035061184471093913
243
+ required_rubygems_version: !ruby/object:Gem::Requirement
244
+ none: false
245
+ requirements:
246
+ - - ! '>='
247
+ - !ruby/object:Gem::Version
248
+ version: '0'
249
+ segments:
250
+ - 0
251
+ hash: -2035061184471093913
252
+ requirements: []
253
+ rubyforge_project:
254
+ rubygems_version: 1.8.25
255
+ signing_key:
256
+ specification_version: 3
257
+ summary: Setting for Rails app with mongoid and RailsAdmin
258
+ test_files:
259
+ - spec/advanced_usage_spec.rb
260
+ - spec/factories/setting.rb
261
+ - spec/model_spec.rb
262
+ - spec/settings_spec.rb
263
+ - spec/spec_helper.rb
264
+ - spec/support/database_cleaner.rb
265
+ - spec/support/mongoid.rb
266
+ - spec/support/mongoid.yml