Skip to content
54 changes: 2 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This gem is not supported and developed anymore.

Easy to use general purpose settings backend for activeadmin.

![ActiveadminSettings Demo](https://raw.github.com/slate-studio/activeadmin-settings/master/img/activeadmin-settings-demo.jpg)
![ActiveadminSettings Demo](https://raw.githubusercontent.com/Yarroo/activeadmin-settings/master/img/activeadmin-settings-demo.jpg)


### Installation
Expand All @@ -26,61 +26,11 @@ Add i18n configuration to `config/application.rb` file:
config.i18n.default_locale = :en
config.i18n.available_locales = [:en, :ru]

#### ActiveRecord

gem "aws-s3"
gem "fog"
gem "mini_magick"
gem "carrierwave"

gem "bson_ext"
gem "devise"
gem "activeadmin-settings"

Run migrations:

$ rake activeadmin_settings:install:migrations
$ rake db:migrate

#### Mongoid 2.x

If you're using mongoid 2.x the gem expects to see **activeadmin-mongoid** and **carrierwave-mongoid** (for image uploading feature) in Gemfile. Here is a working example:

gem "aws-s3"
gem "fog"
gem "mini_magick"
gem "carrierwave-mongoid"

gem "bson_ext"
gem "mongoid"
gem "mongoid-globalize"
gem "devise"
gem "activeadmin-mongoid"
gem "activeadmin-settings"

#### Mongoid 3.x

Here is an example of Gemfile with a support of 3.x version:

# Mongoid 3.x
gem 'moped', git: 'git://github.com/mongoid/moped.git'
gem 'mongoid', '~> 3.0.5'

# Assets
gem 'aws-s3'
gem 'fog'
gem 'mini_magick'
gem 'carrierwave-mongoid', git: 'git://github.com/jnicklas/carrierwave-mongoid.git',
branch: 'mongoid-3.0',
require: 'carrierwave/mongoid'

# Activeadmin
gem 'devise', '>= 2.1.2'
gem 'activeadmin-mongoid', git: 'git://github.com/elia/activeadmin-mongoid.git'
gem "mongoid-globalize"
gem 'activeadmin-settings'


### Configuration

After installation you should find a new **Settings** menu in the admin. If no configuration found in `config/activeadmin_settings.yml` only *Admins* tab is shown. *Admin* tab implements basic functionality of editing *AdminUser* objects.
Expand Down Expand Up @@ -117,7 +67,7 @@ All of the settings may be cplitted to groupes by adding optional param `group`,

As result we'll see:

![ActiveadminSettings Group Example](https://raw.github.com/slate-studio/activeadmin-settings/master/img/activeadmin-group-example.png)
![ActiveadminSettings Group Example](https://raw.githubusercontent.com/Yarroo/activeadmin-settings/master/img/activeadmin-group-example.png)

There are a few types of settings:

Expand Down
5 changes: 1 addition & 4 deletions activeadmin-settings.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
gem.version = ActiveadminSettings::VERSION
gem.authors = ["Alex Kravets"]
gem.email = ["santyor@gmail.com"]
gem.homepage = "https://github.com/slate-studio/activeadmin-settings"
gem.homepage = "https://github.com/Yarroo/activeadmin-settings"
gem.description = "Easy to use general purpose settings backend for activeadmin"
gem.summary = ""

Expand All @@ -15,8 +15,5 @@ Gem::Specification.new do |gem|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})

gem.add_dependency "mini_magick", ">= 3.4"
gem.add_dependency "carrierwave", ">= 1.0"
gem.add_dependency "devise", ">= 4.0"
gem.add_dependency "activeadmin", ">= 2.0"
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ class ActiveadminSettings::AdminUsersController < ApplicationController

def update
@object = AdminUser.find(params[:id])
if @object.update_attributes(permitted_params[:admin_user])
render :text => "ok"
if @object.update(permitted_params[:admin_user])
render :plain => "ok"
else
render :text => @object.errors.to_json, :status => :unprocessable_entity
render :plain => @object.errors.to_json, :status => :unprocessable_entity
end
end

Expand All @@ -15,7 +15,7 @@ def create
if @object.save
render :partial => "admin/settings/admin", :locals => {:admin => @object}, :layout => false
else
render :text => @object.errors.to_json, :status => :unprocessable_entity
render :plain => @object.errors.to_json, :status => :unprocessable_entity
end
end

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/activeadmin_settings/settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ class ActiveadminSettings::SettingsController < ApplicationController
def update
@object = ActiveadminSettings::Setting.find(params[:id])
if @object.update(permitted_params[:setting])
render :text => @object.value
render :plain => @object.value
else
render :text => "error"
render :plain => "error"
end
end

Expand Down
23 changes: 16 additions & 7 deletions app/models/activeadmin_settings/setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def value(name, locale = nil)
end

def reset_snapshot
@snapshot = nil
@snapshot_lock.synchronize { @snapshot = nil }
end

private
Expand All @@ -95,12 +95,21 @@ def snapshot_values
current = @snapshot
return current.values if fresh?(current, ttl)

@snapshot_lock.synchronize do
unless @snapshot_lock.try_lock
return current.values if current

@snapshot_lock.lock
end

begin
current = @snapshot
return current.values if fresh?(current, ttl)

@snapshot = Snapshot.new(load_values, monotonic_now)
@snapshot.values
snapshot = Snapshot.new(load_values, monotonic_now)
@snapshot = snapshot
snapshot.values
ensure
@snapshot_lock.unlock
end
end

Expand All @@ -110,8 +119,8 @@ def fresh?(snapshot, ttl)

def load_values
values = {}
all.each do |setting|
values[[setting.name, setting.locale.to_s]] = setting.value.freeze
unscoped.each do |setting|
values[[setting.name.to_s, setting.locale.to_s]] = setting.value.freeze
rescue StandardError
next
end
Expand All @@ -123,4 +132,4 @@ def monotonic_now
end
end
end
end
end
2 changes: 1 addition & 1 deletion app/views/admin/settings/_settings_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
:as => :text,
:input_html => { :class => "settings-redactor" } %>
<% else %>
<%= f.input :string, :placeholder => "#{t '.default' }: " + setting.default_value(locale) %>
<%= f.input :string, :as => :string, :placeholder => "#{t '.default' }: " + setting.default_value(locale) %>
<% end %>
<% end %>
<% end %>
Expand Down
22 changes: 11 additions & 11 deletions lib/activeadmin-settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ module ActiveadminSettings

# Load configuration from config/activeadmin_settings.yml
def self.load_config
config_file = ::Rails.root.join(@@config_file)
@load_config = {}

if File.exist?(config_file)
data = YAML::load(ERB.new(IO.read(config_file)).result)
@load_config = data if data
@load_config ||= begin
config_file = ::Rails.root.join(@@config_file)
data = YAML::load(ERB.new(IO.read(config_file)).result) if File.exist?(config_file)
data || {}
end
@load_config
end

def self.all_settings
@all_settings = {}
load_config.each do |key, settings|
@all_settings.merge!(settings)
@all_settings ||= load_config.each_with_object({}) do |(_key, settings), all|
all.merge!(settings)
end
@all_settings
end

def self.reload_config!
@load_config = nil
@all_settings = nil
end

def self.groups
Expand Down
2 changes: 1 addition & 1 deletion lib/activeadmin-settings/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ActiveadminSettings
VERSION = "0.5.0"
VERSION = "0.6.0"
end