forked from SpinaCMS/Spina
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP plugin settings * Allow default values * Add plugin level tests * Add controller tests * Merge in latest upstream master * Add test for default values * Change find_by_name to more generic find_by * Clean up a little bit
- Loading branch information
1 parent
388cbb5
commit 29638c3
Showing
19 changed files
with
257 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module Spina | ||
module Admin | ||
class SettingsController < AdminController | ||
|
||
before_action :find_or_set_settings | ||
before_action :set_breadcrumbs | ||
|
||
def edit | ||
add_breadcrumb t("spina.#{plugin.namespace}.title") | ||
end | ||
|
||
def update | ||
if @setting.update_attributes(settings_params) | ||
redirect_to spina.admin_edit_settings_path(plugin.namespace) | ||
else | ||
add_breadcrumb t("spina.#{plugin.namespace}.title") | ||
render :edit | ||
end | ||
end | ||
|
||
private | ||
|
||
def setting_class | ||
"spina/#{plugin.namespace}/setting".classify.constantize | ||
end | ||
|
||
def plugin | ||
Spina::Plugin.find_by(namespace: params[:plugin]) | ||
end | ||
helper_method :plugin | ||
|
||
def find_or_set_settings | ||
@setting = setting_class.first_or_create do |setting| | ||
plugin.settings.each do |attribute, type| | ||
setting.send("#{attribute}=", (type.is_a?(Hash) ? type.first.last : nil)) | ||
end | ||
end | ||
plugin.settings.keys.reject do |x| | ||
@setting.preferences.keys.map(&:to_sym).include? x | ||
end.each do |key| | ||
value = plugin.settings[key].is_a?(Hash) ? plugin.settings[key].first.last : nil | ||
@setting.send("#{key}=", value) | ||
end | ||
end | ||
|
||
def set_breadcrumbs | ||
add_breadcrumb t('spina.settings.title') | ||
end | ||
|
||
def settings_params | ||
params.require(:setting).permit(plugin.settings.keys) | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Spina | ||
class Setting < ApplicationRecord | ||
|
||
validates :plugin, presence: true | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.horizontal-form-label | ||
= f.label attribute, t("spina.#{plugin.namespace}.settings.#{attribute}") | ||
.horizontal-form-content | ||
= f.text_field attribute, placeholder: t("spina.#{plugin.namespace}.settings.#{attribute}_placeholder") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.horizontal-form-label | ||
= f.label attribute, t("spina.#{plugin.namespace}.settings.#{attribute}") | ||
.horizontal-form-content | ||
= render 'spina/admin/shared/rich_text_field', f: f, field: attribute |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
= form_for @setting, url: admin_settings_path do |f| | ||
%header#header | ||
= render partial: 'spina/admin/shared/breadcrumbs' | ||
|
||
#header_actions | ||
%button.button.button-primary{type: 'submit'} | ||
= icon('check') | ||
= t('spina.settings.save') | ||
|
||
.well | ||
.horizontal-form | ||
- plugin.settings.each do |key, value| | ||
.horizontal-form-group= render "#{value.is_a?(Hash) ? value.first.first : value}_field", f: f, attribute: key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreateSpinaSettings < ActiveRecord::Migration[5.0] | ||
def change | ||
create_table :spina_settings do |t| | ||
t.string :plugin | ||
t.jsonb :preferences, default: {} | ||
t.timestamps | ||
end | ||
|
||
add_index :spina_settings, :plugin | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require 'test_helper' | ||
|
||
module Spina | ||
module Admin | ||
class SettingsControllerTest < ActionController::TestCase | ||
setup do | ||
@routes = ::Spina::Engine.routes | ||
@account = FactoryGirl.create :account | ||
@user = FactoryGirl.create :user | ||
@plugin = ::Spina::Plugin.find_by(namespace: 'spina_test') | ||
@controller.stubs(:current_spina_user).returns(@user) | ||
end | ||
|
||
test 'editing produces the correct forms' do | ||
get :edit, params: { plugin: 'spina_test' } | ||
assert_template :edit | ||
assert assigns(:setting).is_a?(Spina::SpinaTest::Setting) | ||
assert assigns(:setting).test_setting == '<div></div>' | ||
end | ||
|
||
test 'updating updates the settings' do | ||
patch :update, params: { plugin: 'spina_test', setting: { foobar: 'baz' } } | ||
|
||
assert Spina::SpinaTest::Setting.first.foobar == 'baz' | ||
end | ||
|
||
end | ||
end | ||
end |
Oops, something went wrong.