From 7bc17ca3ec1f90530f0923a6fc04742b758ca997 Mon Sep 17 00:00:00 2001 From: Devon Noonan Date: Wed, 3 Oct 2012 01:40:31 -0400 Subject: [PATCH] Generators and config files --- .../configuration/configuration_generator.rb | 14 ++++++++++ .../configuration/templates/sisyphus.yml | 13 +++++++++ lib/sisyphus-rails.rb | 5 ++++ lib/sisyphus-rails/engine.rb | 27 +++++++++++++++++++ lib/sisyphus-rails/form_helper.rb | 15 ++++++++--- lib/sisyphus-rails/form_tag_helper.rb | 2 ++ 6 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 lib/generators/sisyphus/configuration/configuration_generator.rb create mode 100644 lib/generators/sisyphus/configuration/templates/sisyphus.yml diff --git a/lib/generators/sisyphus/configuration/configuration_generator.rb b/lib/generators/sisyphus/configuration/configuration_generator.rb new file mode 100644 index 0000000..941b622 --- /dev/null +++ b/lib/generators/sisyphus/configuration/configuration_generator.rb @@ -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 \ No newline at end of file diff --git a/lib/generators/sisyphus/configuration/templates/sisyphus.yml b/lib/generators/sisyphus/configuration/templates/sisyphus.yml new file mode 100644 index 0000000..55f9e17 --- /dev/null +++ b/lib/generators/sisyphus/configuration/templates/sisyphus.yml @@ -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 \ No newline at end of file diff --git a/lib/sisyphus-rails.rb b/lib/sisyphus-rails.rb index ac49ba8..d87d321 100644 --- a/lib/sisyphus-rails.rb +++ b/lib/sisyphus-rails.rb @@ -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 \ No newline at end of file diff --git a/lib/sisyphus-rails/engine.rb b/lib/sisyphus-rails/engine.rb index a2b4dc5..993bfe3 100644 --- a/lib/sisyphus-rails/engine.rb +++ b/lib/sisyphus-rails/engine.rb @@ -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 \ No newline at end of file diff --git a/lib/sisyphus-rails/form_helper.rb b/lib/sisyphus-rails/form_helper.rb index ccfdf37..8a14264 100644 --- a/lib/sisyphus-rails/form_helper.rb +++ b/lib/sisyphus-rails/form_helper.rb @@ -2,11 +2,18 @@ module ActionView module Helpers module FormHelper def form_for_with_sisyphus(record, options = {}, &proc) - - if options[:with_sisyphus] == false + + # 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 diff --git a/lib/sisyphus-rails/form_tag_helper.rb b/lib/sisyphus-rails/form_tag_helper.rb index 5a98156..7e8adfa 100644 --- a/lib/sisyphus-rails/form_tag_helper.rb +++ b/lib/sisyphus-rails/form_tag_helper.rb @@ -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("") end