Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add very basic initializer to enforce system wide #116

Open
wants to merge 1 commit into from

1 participant

@bemurphy

I've added the config initializer referenced in the README on every
Rails 3.x project I've deployed StrongParameters in, so figured
this is useful.

@bemurphy bemurphy Add very basic initializer to enforce system wide
I've added the config initializer referenced in the README on every
Rails 3.x project I've deployed StrongParameters in, so figured
this is useful.
2f58b50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 10, 2013
  1. @bemurphy

    Add very basic initializer to enforce system wide

    bemurphy authored
    I've added the config initializer referenced in the README on every
    Rails 3.x project I've deployed StrongParameters in, so figured
    this is useful.
This page is out of date. Refresh to see the latest.
View
15 lib/generators/rails/strong_parameters_initializer_generator.rb
@@ -0,0 +1,15 @@
+module Rails
+ module Generators
+ class StrongParametersInitializerGenerator < Rails::Generators::Base
+ desc "Creates an initializer file to enforce strong parameters on all ActiveRecord models"
+
+ def create_initializer_file
+ create_file "config/initializers/strong_parameters.rb", initializer_content
+ end
+
+ def initializer_content
+ "ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)\n"
+ end
+ end
+ end
+end
View
18 test/initializer_generator_test.rb
@@ -0,0 +1,18 @@
+require 'rails/generators/test_case'
+require 'generators/rails/strong_parameters_initializer_generator'
+
+class StrongParametersInitializerGeneratorTest < Rails::Generators::TestCase
+ tests Rails::Generators::StrongParametersInitializerGenerator
+ destination File.expand_path("../tmp", File.dirname(__FILE__))
+ setup :prepare_destination
+
+ def test_initializer_content
+ Rails.stubs(:application).returns(nil)
+ run_generator
+
+ assert_file "config/initializers/strong_parameters.rb" do |content|
+ init_code = 'ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)'
+ assert_match init_code, content
+ end
+ end
+end
Something went wrong with that request. Please try again.