activeadmin-settings 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (29) hide show
  1. data/.gitignore +1 -0
  2. data/README.md +11 -1
  3. data/activeadmin-settings.gemspec +0 -3
  4. data/app/controller/activeadmin_settings/admin_users_controller.rb +27 -0
  5. data/app/controller/activeadmin_settings/settings_controller.rb +12 -0
  6. data/app/views/admin/settings/_admin.html.erb +42 -0
  7. data/app/views/admin/settings/_admin_form.html.erb +22 -0
  8. data/app/views/admin/settings/_admins_table.html.erb +16 -0
  9. data/app/views/admin/settings/_index.html.erb +20 -0
  10. data/app/views/admin/settings/_settings_table.html.erb +48 -0
  11. data/config/routes.rb +4 -0
  12. data/lib/activeadmin-settings.rb +22 -3
  13. data/lib/activeadmin-settings/helper.rb +5 -2
  14. data/lib/activeadmin-settings/version.rb +1 -1
  15. data/lib/generators/activeadmin_settings/install_generator.rb +4 -5
  16. data/lib/generators/activeadmin_settings/templates/admin/settings.rb +17 -38
  17. data/lib/generators/activeadmin_settings/templates/config/activeadmin_settings.yml +8 -6
  18. data/lib/generators/activeadmin_settings/templates/models/setting.rb +3 -3
  19. data/lib/tasks/activeadmin_settings.rake +15 -0
  20. data/vendor/assets/javascripts/activeadmin_settings.js.coffee +2 -32
  21. data/vendor/assets/javascripts/activeadmin_settings/admins.js.coffee +80 -0
  22. data/vendor/assets/javascripts/activeadmin_settings/flashes.js.coffee +6 -0
  23. data/vendor/assets/javascripts/activeadmin_settings/settings.js.coffee +32 -0
  24. data/vendor/assets/javascripts/activeadmin_settings/tabs.js.coffee +6 -0
  25. data/vendor/assets/javascripts/jquery.easytabs.js +679 -0
  26. data/vendor/assets/stylesheets/activeadmin_settings.css.scss +14 -1
  27. metadata +16 -5
  28. data/app/views/admin/settings/_value.html.erb +0 -21
  29. data/lib/generators/activeadmin_settings/templates/admin/admin_users.rb +0 -23
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  .DS_Store
2
+ pkg
data/README.md CHANGED
@@ -1,3 +1,13 @@
1
1
  ## activeadmin-settings
2
2
 
3
- Easy to use general purpose settings backend for activeadmin.
3
+ Easy to use general purpose settings backend for activeadmin.
4
+
5
+ ### Installation
6
+
7
+ Add to `Gemfile`:
8
+
9
+ gem "activeadmin-settings"
10
+
11
+ Run installer:
12
+
13
+ $ rails g activeadmin_settings:install
@@ -14,7 +14,4 @@ Gem::Specification.new do |gem|
14
14
  gem.name = "activeadmin-settings"
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = ActiveadminSettings::VERSION
17
-
18
- #gem.add_dependency("mime-types")
19
- #gem.add_dependency("orm_adapter")
20
17
  end
@@ -0,0 +1,27 @@
1
+ class ActiveadminSettings::AdminUsersController < ApplicationController
2
+ before_filter :authenticate_admin_user!
3
+
4
+ def update
5
+ @object = AdminUser.find(params[:id])
6
+ if @object.update_attributes(params[:admin_user])
7
+ render :text => "ok"
8
+ else
9
+ render :text => @object.errors.to_json, :status => :unprocessable_entity
10
+ end
11
+ end
12
+
13
+ def create
14
+ @object = AdminUser.new(params[:admin_user])
15
+ if @object.save
16
+ render :partial => "admin/settings/admin", :locals => {:admin => @object}, :layout => false
17
+ else
18
+ render :text => @object.errors.to_json, :status => :unprocessable_entity
19
+ end
20
+ end
21
+
22
+ def destroy
23
+ @object = AdminUser.find(params[:id])
24
+ @object.destroy
25
+ redirect_to "/admin/settings#admins"
26
+ end
27
+ end
@@ -0,0 +1,12 @@
1
+ class ActiveadminSettings::SettingsController < ApplicationController
2
+ before_filter :authenticate_admin_user!
3
+
4
+ def update
5
+ @object = ActiveadminSettings::Setting.find(params[:id])
6
+ if @object.update_attributes(params[:setting])
7
+ render :text => @object.value
8
+ else
9
+ render :text => "error"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,42 @@
1
+ <tr class="odd admin" id="admin_<%= admin.id %>">
2
+ <td class="">
3
+ <strong><%= admin.email %></strong>
4
+ <div class="form">
5
+ <br/>
6
+ <%= semantic_form_for admin,
7
+ :method => :put,
8
+ :url => "/admin/admin_users/#{admin.id}" do |f| %>
9
+ <%= f.inputs do %>
10
+ <%= f.input :password, :as => :password, :required => true %>
11
+ <%= f.input :password_confirmation, :as => :password, :required => true %>
12
+ <% end %>
13
+ <% end %>
14
+ </div>
15
+ </td>
16
+ <td>
17
+ <span class="admin_actions">
18
+ <a href="#" class="member_link credentials_link"
19
+ data-id="#admin_<%= admin.id %>">
20
+ Credentials
21
+ </a>
22
+ <a href="/admin/admin_users/<%= admin.id %>"
23
+ class="member_link delete_link"
24
+ data-method="delete"
25
+ data-confirm="Are you sure?"
26
+ data-id="#admin_<%= admin.id %>">
27
+ Delete
28
+ </a>
29
+ </span>
30
+
31
+ <span class="form_actions">
32
+ <a href="#" class="member_link update_link"
33
+ data-id="#admin_<%= admin.id %>">
34
+ Update
35
+ </a>
36
+ <a href="#" class="member_link cancel_link"
37
+ data-id="#admin_<%= admin.id %>">
38
+ Cancel
39
+ </a>
40
+ </span>
41
+ </td>
42
+ </tr>
@@ -0,0 +1,22 @@
1
+ <tr class="odd">
2
+ <td class="">
3
+ <div class="form new_admin">
4
+ <%= semantic_form_for AdminUser.new,
5
+ :method => :post,
6
+ :url => "/admin/admin_users" do |f| %>
7
+ <%= f.inputs do %>
8
+ <%= f.input :email, :required => true %>
9
+ <%= f.input :password, :required => true %>
10
+ <%= f.input :password_confirmation, :required => true %>
11
+ <% end %>
12
+ <% end %>
13
+ </div>
14
+ </td>
15
+ <td class="new_admin_actions">
16
+ <a href="#" class="member_link new_link">New Admin</a>
17
+ <span class="form_actions">
18
+ <a href="#" class="member_link create_link">Create</a>
19
+ <a href="#" class="member_link cancel_link">Cancel</a>
20
+ </span>
21
+ </td>
22
+ </tr>
@@ -0,0 +1,16 @@
1
+ <table border="0"
2
+ cellspacing="0"
3
+ cellpadding="0"
4
+ id="admins"
5
+ class="index_table index">
6
+ <thead>
7
+ <tr>
8
+ <th class="">Email</th>
9
+ <th class=""></th>
10
+ </tr>
11
+ </thead>
12
+ <tbody>
13
+ <%= render :partial => "admin", :collection => admins %>
14
+ <%= render :partial => "admin_form" %>
15
+ </tbody>
16
+ </table>
@@ -0,0 +1,20 @@
1
+ <div id="settings_tabs">
2
+ <ul class="tabs table_tools_segmented_control">
3
+ <% groups.each do |g| %>
4
+ <li>
5
+ <a class="table_tools_button" href="#<%= g[:slug] %>"><%= g[:name] %></a>
6
+ </li>
7
+ <% end %>
8
+ <li><a class="table_tools_button" href="#admins">Admins</a></li>
9
+ </ul>
10
+
11
+ <% groups.each do |g| %>
12
+ <div id="<%= g[:slug] %>">
13
+ <%= render :partial => "settings_table", :locals => {:settings => g[:settings]} %>
14
+ </div>
15
+ <% end %>
16
+
17
+ <div id="admins">
18
+ <%= render :partial => "admins_table", :locals => {:admins => admins} %>
19
+ </div>
20
+ </div>
@@ -0,0 +1,48 @@
1
+ <table border="0"
2
+ cellspacing="0"
3
+ cellpadding="0"
4
+ id="settings"
5
+ class="index_table index">
6
+ <thead>
7
+ <tr>
8
+ <th class="">Name</th>
9
+ <th class="">Value</th>
10
+ <th class=""></th>
11
+ </tr>
12
+ </thead>
13
+ <tbody>
14
+ <% settings.each do |setting| %>
15
+ <tr class="odd" id="setting_<%= setting.id %>">
16
+ <td class="">
17
+ <strong><%= setting.name %></strong></br>
18
+ <em><%= setting.description %></em>
19
+ </td>
20
+ <td class="">
21
+ <div class="form <%= setting.type %>">
22
+ <%= semantic_form_for setting,
23
+ :method => :put,
24
+ :url => "/admin/settings/#{setting.id}" do |f| %>
25
+ <%= f.inputs do %>
26
+ <% case setting.type %>
27
+ <% when "file" %>
28
+ <%= f.input :file, :hint => setting.value %>
29
+ <%= f.input :remove_file, :as => :boolean %>
30
+ <% when "text" %>
31
+ <%= f.input :string, :as => :text %>
32
+ <% else %>
33
+ <%= f.input :string, :placeholder => "Default: " + setting.default_value %>
34
+ <% end %>
35
+ <% end %>
36
+ <% end %>
37
+ </div>
38
+ </td>
39
+ <td class="">
40
+ <a href="#" class="member_link update_link"
41
+ data-id="#setting_<%= setting.id %>">
42
+ Update
43
+ </a>
44
+ </td>
45
+ </tr>
46
+ <% end %>
47
+ </tbody>
48
+ </table>
@@ -0,0 +1,4 @@
1
+ ActiveadminSettings::Engine.routes.draw do
2
+ resources :settings, :only => [:update]
3
+ resources :admin_users, :only => [:create, :update, :destroy]
4
+ end
@@ -4,13 +4,32 @@ module ActiveadminSettings
4
4
  require 'activeadmin-settings/engine'
5
5
  require 'activeadmin-settings/helper'
6
6
 
7
- def self.settings
7
+ def self.load_config
8
8
  config_file = ::Rails.root.join("config/activeadmin_settings.yml")
9
- @settings = {}
9
+ @load_config = {}
10
10
 
11
11
  if File.exists?(config_file)
12
12
  data = YAML::load(ERB.new(IO.read(config_file)).result)
13
- @settings = data if data
13
+ @load_config = data if data
14
14
  end
15
15
  end
16
+
17
+ def self.all_settings
18
+ @all_settings = {}
19
+ load_config.each do |key, settings|
20
+ @all_settings.merge!(settings)
21
+ end
22
+ @all_settings
23
+ end
24
+
25
+ def self.groups
26
+ @groups = []
27
+ load_config.each do |key, settings|
28
+ @groups << { :name => key,
29
+ :slug => key.downcase.gsub(" ", "_"),
30
+ :default_settings => settings,
31
+ :settings => [] }
32
+ end
33
+ @groups
34
+ end
16
35
  end
@@ -1,10 +1,10 @@
1
1
  module ActiveadminSettings
2
2
  module Helpers
3
- def setting_tag(name)
3
+ def settings_value(name)
4
4
  Setting.find_or_create_by(name: name).value
5
5
  end
6
6
 
7
- def link_setting_tag(name, html_options={})
7
+ def settings_link_value(name, html_options={})
8
8
  val = Setting.find_or_create_by(name: name).value
9
9
 
10
10
  if not val.empty? # add regular expression check here
@@ -15,5 +15,8 @@ module ActiveadminSettings
15
15
  link_to(title, url, html_options)
16
16
  end
17
17
  end
18
+
19
+ module_function :settings_value, :settings_link_value
18
20
  end
19
21
  end
22
+
@@ -1,3 +1,3 @@
1
1
  module ActiveadminSettings
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -16,7 +16,6 @@ module ActiveadminSettings
16
16
 
17
17
  # admin
18
18
  puts "Installing admin:"
19
- copy_file "admin/admin_users.rb", "app/admin/admin_users.rb"
20
19
  copy_file "admin/settings.rb", "app/admin/settings.rb"
21
20
 
22
21
  # config
@@ -24,6 +23,10 @@ module ActiveadminSettings
24
23
  copy_file "config/activeadmin_settings.yml", "config/activeadmin_settings.yml"
25
24
  end
26
25
 
26
+ def mount_engine
27
+ route "mount ActiveadminSettings::Engine => '/admin'"
28
+ end
29
+
27
30
  def add_assets
28
31
  if File.exist?('app/assets/javascripts/active_admin.js')
29
32
  insert_into_file "app/assets/javascripts/active_admin.js",
@@ -39,10 +42,6 @@ module ActiveadminSettings
39
42
  puts "It doesn't look like you've installed activeadmin: active_admin.scss is missing.\nPlease install it and try again."
40
43
  end
41
44
  end
42
-
43
- def add_gems
44
- gem "activeadmin-mongoid-reorder"
45
- end
46
45
  end
47
46
  end
48
47
  end
@@ -1,44 +1,23 @@
1
- ActiveAdmin.register ActiveadminSettings::Setting, :as => "Setting" do
2
- menu :parent => "Settings", :priority => 1, :label => "General"
1
+ ActiveAdmin.register_page "Settings" do
2
+ content do
3
+ # Mergin db settings objects and default values
4
+ # from config/activaadmin_settings.yml file.
3
5
 
4
- actions :index, :update, :destroy
5
-
6
- controller do
7
- def update
8
- update! do |format|
9
- format.html { render :text => resource.value }
10
- end
11
- end
12
- end
13
-
14
- index :as => :reorder_table do
15
- column :name do |s|
16
- "<strong>#{s.name}</strong><br/><em>#{s.description}</em>".html_safe
17
- end
18
-
19
- column :value do |s|
20
- render :partial => "value", :locals => {:setting => s}
6
+ all_settings = {}
7
+ ActiveadminSettings::Setting.all.each do |s|
8
+ all_settings[s.name] = s
21
9
  end
22
10
 
23
- column "" do |s|
24
- link_to("Edit", "#", :class => "member_link edit_setting_link", "data-setting-id" => s.id) +
25
- link_to("Update", "#", :class => "member_link update_setting_link", "data-setting-id" => s.id) +
26
- link_to("Delete", admin_setting_path(s), :class => "member_link", :method => :delete, :confirm => "Are you sure?")
27
- end
28
- end
29
-
30
- form do |f|
31
- f.inputs do
32
- f.input :name
33
- f.input :type
34
- f.input :description
35
- f.input :string
11
+ groups = ActiveadminSettings.groups
12
+ groups.each do |g|
13
+ g[:default_settings].each_key do |name|
14
+ s = all_settings[name]
15
+ g[:settings] << s if s
16
+ end
36
17
  end
37
18
 
38
- f.buttons
39
- end
40
-
41
- collection_action :reorder, :method => :put do
42
- render :text => resource_class.reorder_objects(params[:ids])
19
+ render :partial => "index",
20
+ :locals => { :admins => AdminUser.all,
21
+ :groups => groups}
43
22
  end
44
- end
23
+ end
@@ -3,13 +3,15 @@
3
3
  # ---configuration file v1.0---
4
4
  # ================================================================================
5
5
  #
6
- # This file is used to store a list of activeadmin settings.
7
- #
6
+ # This file is used to store a list of activeadmin settings. Settings are split
7
+ # into groups like this:
8
+ #
8
9
  # Here is a setting template:
9
- # Setting Title:
10
- # type: string (default) / text / file / link
11
- # description: setting one sentence description
12
- # default_value: default value is used when setting value is blank
10
+ # General Group:
11
+ # Setting Title:
12
+ # type: string (default) / text / file / link
13
+ # description: setting one sentence description
14
+ # default_value: default value is used when setting value is blank
13
15
  #
14
16
  # Type details:
15
17
  # Link setting value expected to be in a format of: (link title) http://link.url
@@ -18,15 +18,15 @@ class ActiveadminSettings::Setting
18
18
 
19
19
  # Instance
20
20
  def type
21
- ActiveadminSettings.settings[name]["type"] ||= "string"
21
+ ActiveadminSettings.all_settings[name]["type"] ||= "string"
22
22
  end
23
23
 
24
24
  def description
25
- ActiveadminSettings.settings[name]["description"] ||= ""
25
+ ActiveadminSettings.all_settings[name]["description"] ||= ""
26
26
  end
27
27
 
28
28
  def default_value
29
- ActiveadminSettings.settings[name]["default_value"] ||= ""
29
+ ActiveadminSettings.all_settings[name]["default_value"] ||= ""
30
30
  end
31
31
 
32
32
  def value
@@ -8,4 +8,19 @@ namespace :activeadmin do
8
8
  email: admin@example.com
9
9
  password: password"
10
10
  end
11
+
12
+ desc "Add missing settings from activeadmin_settings.yml file to the db"
13
+ task :add_settings => :environment do
14
+ ActiveadminSettings.all_settings.each_key |name|
15
+ Setting.find_or_create_by(name: name)
16
+ end
17
+ end
18
+
19
+ desc "Add settings from activeadmin_settings.yml file to the db"
20
+ task :flush_settings => :environment do
21
+ ActiveadminSettings::Setting.all.destroy
22
+ ActiveadminSettings.all_settings.each_key |name|
23
+ Setting.find_or_create_by(name: name)
24
+ end
25
+ end
11
26
  end