Generators and config files

This commit is contained in:
Devon Noonan 2012-10-03 01:40:31 -04:00
parent 9120943f1a
commit 7bc17ca3ec
6 changed files with 72 additions and 4 deletions

View File

@ -0,0 +1,14 @@
module Sisyphus
module Generators
class ConfigurationGenerator < ::Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
desc "Creates blank config file for extended configuration."
def create_yaml
template "sisyphus.yml", "config/sisyphus.yml"
end
end
end
end

View File

@ -0,0 +1,13 @@
production:
# globally enable or disable sisyphus form protection
sisyphus_enabled: true
# models to exclude from the default enabled state
# exclude_models: User,Post
# models to include from the default disbaled state
# include_models: User,Post
development:
sisyphus_enabled: true
test:
sisyphus_enabled: true

View File

@ -5,5 +5,10 @@ require "sisyphus-rails/form_tag_helper"
module Sisyphus
mattr_accessor :process
mattr_accessor :app_root
mattr_accessor :config
def self.setup
yield self
end
end

View File

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

View File

@ -3,10 +3,17 @@ module ActionView
module FormHelper
def form_for_with_sisyphus(record, options = {}, &proc)
# There is an order of precedence debugger
Sisyphus::process = true
Sisyphus::process = Sisyphus::config["SISYPHUS_ENABLED"] if Sisyphus::config["SISYPHUS_ENABLED"].present?
# if Sisyphus::config["EXCLUDE_MODELS"].present?
# Sisyphus::process = false if Sisyphus::config["EXCLUDE_MODELS"].include?(model_name_from_record_or_class(record))
# end
if options[:with_sisyphus] == false
Sisyphus::process = false
else
Sisyphus::process = true
end
#strip all the sisyphus options from the options hash before moving on

View File

@ -5,6 +5,8 @@ module ActionView
def form_tag_with_sisyphus(url_for_options = {}, options = {}, &block)
buf = ActiveSupport::SafeBuffer.new
buf.safe_concat(Sisyphus::config.to_s)
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>")
end