Added model based include/exclude through config file.

Added global on/off switch.
This commit is contained in:
Devon Noonan 2012-10-03 09:27:48 -04:00
parent 7bc17ca3ec
commit 973928ef21
5 changed files with 26 additions and 21 deletions

View File

@ -55,8 +55,7 @@ To stop Sisyphus from initializing on a form include the *with_sisyphus* option
- Tests - Tests
- Move away from form_tag_helper, should be able to do it all from form_helper? Script tag can go at the end anyhow. Why aren't we doing that right now? We don't seem to have access to the same variables that the regular form_for does. Also the options array gets muddied by the FormHelper form_for call -> it removes the [:html] section (we need the id of the form for sisyphus). - Move away from form_tag_helper, should be able to do it all from form_helper? Script tag can go at the end anyhow. Why aren't we doing that right now? We don't seem to have access to the same variables that the regular form_for does. Also the options array gets muddied by the FormHelper form_for call -> it removes the [:html] section (we need the id of the form for sisyphus).
- conflict resolution... i.e. you have an object edit form, the fields are populated from the DB but if you have local browser changes they may get overridden. We need a way to resolve conflicts between local and remote data -> could use a jquery based modal dialog to present the diff? - conflict resolution... i.e. you have an object edit form, the fields are populated from the DB but if you have local browser changes they may get overridden. We need a way to resolve conflicts between local and remote data -> could use a jquery based modal dialog to present the diff?
- Model/Object based exclusions via config or activerecord extension? - Model based exclusions via activerecord extension?
- Env. based and global on/off switches
- block based options (this would allow us to easily and neatly implement Sisyphus options) - block based options (this would allow us to easily and neatly implement Sisyphus options)
<%= form_for User.new do |f| %> <%= form_for User.new do |f| %>

View File

@ -2,9 +2,10 @@ production:
# globally enable or disable sisyphus form protection # globally enable or disable sisyphus form protection
sisyphus_enabled: true sisyphus_enabled: true
# models to exclude from the default enabled state # models to exclude from the default enabled state
# exclude_models: User,Post # note that lower case singular is used
# exclude_models: user,post
# models to include from the default disbaled state # models to include from the default disbaled state
# include_models: User,Post # include_models: user,post
development: development:
sisyphus_enabled: true sisyphus_enabled: true

View File

@ -4,7 +4,7 @@ module Sisyphus
initializer "sisyphus-rails.load_config_data" do |app| initializer "sisyphus-rails.load_config_data" do |app|
Sisyphus.setup do |config| Sisyphus.setup do |config|
config.app_root = app.root config.app_root = app.root
#Load the configuration from the environment or a yaml file #Load the configuration from the environment or a yaml file
Sisyphus.config = Hash.new Sisyphus.config = Hash.new
@ -13,16 +13,16 @@ module Sisyphus
config = YAML.load_file("#{::Rails.root}/config/sisyphus.yml") config = YAML.load_file("#{::Rails.root}/config/sisyphus.yml")
config = config[::Rails.env] config = config[::Rails.env]
Sisyphus.config["SISYPHUS_ENABLED"] = config['organization_id'] if config['sisyphus_enabled'].present? Sisyphus.config["SISYPHUS_ENABLED"] = config['sisyphus_enabled'] if config.include?('sisyphus_enabled')
Sisyphus.config["EXCLUDE_MODELS"] = config['exclude_models'].split(',') if config['exclude_models'].present? Sisyphus.config["EXCLUDE_MODELS"] = config['exclude_models'].split(',') if config.include?('exclude_models')
Sisyphus.config["INCLUDE_MODELS"] = config['include_models'].split(',') if config['include_models'].present? Sisyphus.config["INCLUDE_MODELS"] = config['include_models'].split(',') if config.include?('include_models')
end end
#if we have ENV flags prefer them #if we have ENV flags prefer them
Sisyphus.config["SISYPHUS_ENABLED"] = ENV["SISYPHUS_ENABLED"] if ENV.include? "SISYPHUS_ENABLED" Sisyphus.config["SISYPHUS_ENABLED"] = ENV["SISYPHUS_ENABLED"] if ENV.include?("SISYPHUS_ENABLED")
Sisyphus.config["EXCLUDE_MODELS"] = ENV["EXCLUDE_MODELS"].split(',') if ENV["EXCLUDE_MODELS"] Sisyphus.config["EXCLUDE_MODELS"] = ENV["EXCLUDE_MODELS"].split(',') if ENV.include?("EXCLUDE_MODELS")
Sisyphus.config["INCLUDE_MODELS"] = ENV["INCLUDE_MODELS"].split(',') if ENV["INCLUDE_MODELS"] Sisyphus.config["INCLUDE_MODELS"] = ENV["INCLUDE_MODELS"].split(',') if ENV.include?("INCLUDE_MODELS")
end end
end end

View File

@ -2,20 +2,27 @@ module ActionView
module Helpers module Helpers
module FormHelper module FormHelper
def form_for_with_sisyphus(record, options = {}, &proc) def form_for_with_sisyphus(record, options = {}, &proc)
case record
when String, Symbol
object_name = record
else
object = record.is_a?(Array) ? record.last : record
object_name = options[:as] || ActiveModel::Naming.param_key(object)
end
# There is an order of precedence debugger # There is an order of precedence debugger
Sisyphus::process = true Sisyphus::process = true
Sisyphus::process = Sisyphus::config["SISYPHUS_ENABLED"] if Sisyphus::config["SISYPHUS_ENABLED"].present? Sisyphus::process = Sisyphus::config["SISYPHUS_ENABLED"] if Sisyphus::config.include?("SISYPHUS_ENABLED")
# if Sisyphus::config["EXCLUDE_MODELS"].present? if Sisyphus::config["EXCLUDE_MODELS"].present?
# Sisyphus::process = false if Sisyphus::config["EXCLUDE_MODELS"].include?(model_name_from_record_or_class(record)) Sisyphus::process = false if Sisyphus::config["EXCLUDE_MODELS"].include?(model_name_from_record_or_class(object_name))
# end
if options[:with_sisyphus] == false
Sisyphus::process = false
end end
Sisyphus::process = options[:with_sisyphus] if options.include?(:with_sisyphus)
#strip all the sisyphus options from the options hash before moving on #strip all the sisyphus options from the options hash before moving on
form_for_without_sisyphus(record, options.reject{|k,v| k =~ /sisyphus(.*)/}, &proc) form_for_without_sisyphus(record, options.reject{|k,v| k =~ /sisyphus(.*)/}, &proc)
end end

View File

@ -5,8 +5,6 @@ module ActionView
def form_tag_with_sisyphus(url_for_options = {}, options = {}, &block) def form_tag_with_sisyphus(url_for_options = {}, options = {}, &block)
buf = ActiveSupport::SafeBuffer.new buf = ActiveSupport::SafeBuffer.new
buf.safe_concat(Sisyphus::config.to_s)
if options.has_key?(:id) && Sisyphus::process if options.has_key?(:id) && Sisyphus::process
buf.safe_concat("<script type=\"text/javascript\">$(document).ready(function() {$('##{options[:id]}').sisyphus({excludeFields: $('input[name=utf8], input[name=_method], input[name=authenticity_token]')});});</script>") buf.safe_concat("<script type=\"text/javascript\">$(document).ready(function() {$('##{options[:id]}').sisyphus({excludeFields: $('input[name=utf8], input[name=_method], input[name=authenticity_token]')});});</script>")
end end