Skip to content
Browse files

INitial commit 2!

  • Loading branch information...
1 parent e37b9ad commit ffc468bd353fae5ffee9ef858c612c318ec5fb4c @eac committed
View
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2008 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
22 Rakefile
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the attr_inaccessible plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the attr_inaccessible plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'AttrInaccessible'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
View
3 init.rb
@@ -0,0 +1,3 @@
+ActiveRecord::Base.send(:extend, EricChapweske::InaccessibleAttributes)
+ActiveRecord::Base.send(:include, EricChapweske::MassAssignmentLogging)
+ActiveRecord::Migration.send(:extend, EricChapweske::MigrationWarning)
View
1 install.rb
@@ -0,0 +1 @@
+# Install hook code here
View
29 lib/eric_chapweske/inaccessible_attributes.rb
@@ -0,0 +1,29 @@
+module EricChapweske
+ module InaccessibleAttributes
+
+ def self.extended(base)
+ base.disable_mass_assignment
+ end
+
+ # If true, requires each attribute to be specified during mass assignment and effectively disables attr_protected.
+ def disable_mass_assignment(disable = true)
+ disable ? enable_attr_inaccessible : disable_attr_inaccessible
+ end
+
+ private
+ # Create an empty collection of accessible_attributes
+ def enable_attr_inaccessible
+ attr_accessible '_none_by_default'
+ accessible_attributes.delete('_none_by_default')
+ end
+
+ def disable_attr_inaccessible
+ if accessible_attributes.blank?
+ write_inheritable_attribute('attr_accessible', nil)
+ else
+ raise "attr_accessible already defined for #{ accessible_attributes.join(', ') }. Can't disable"
+ end
+ end
+
+ end
+end
View
20 lib/eric_chapweske/mass_assignment_logging.rb
@@ -0,0 +1,20 @@
+module EricChapweske
+ module MassAssignmentLogging
+
+ def self.included(base)
+ base.class_eval do
+ alias_method_chain :remove_attributes_protected_from_mass_assignment, :noticable_logging
+ end
+ end
+
+ # Adds an additional, red-colored debug message when mass assignment fails with an attribute.
+ def remove_attributes_protected_from_mass_assignment_with_noticable_logging(attributes)
+ safe_attributes = remove_attributes_protected_from_mass_assignment_without_noticable_logging(attributes)
+ unless attributes == safe_attributes
+ logger.debug "\e[9;31;1mValues assigned to those attributes were ignored!\e[0m"
+ end
+ safe_attributes
+ end
+
+ end
+end
View
24 lib/eric_chapweske/migration_warning.rb
@@ -0,0 +1,24 @@
+module EricChapweske
+ module MigrationWarning #ActiveRecord::Migration
+
+ def self.extended(base)
+ class << base
+ alias_method_chain :migrate, :mass_assignment_warning
+ end
+ end
+
+ # Adds a warning message during migrations.
+ # Example:
+ # == 57 CreateUsers: New attributes will be ignored by mass assignment unless `attr_accessible :attribute` is specified in their model.
+ # == 57 CreateUsers: migrating ========================================================
+ # == 57 CreateUsers: migrated (0.0001s) ===============================================
+
+ def migrate_with_mass_assignment_warning(direction)
+ if direction == :up
+ announce "New attributes will be ignored by mass assignment unless `attr_accessible :attribute` is specified in their model."
+ end
+ migrate_without_mass_assignment_warning(direction)
+ end
+
+ end
+end
View
4 tasks/attr_inaccessible_tasks.rake
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :attr_inaccessible do
+# # Task goes here
+# end
View
76 test/inaccessible_attributes_test.rb
@@ -0,0 +1,76 @@
+require 'test/unit'
+require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
+require 'rubygems'
+
+
+class InaccessibleAttributesTest < Test::Unit::TestCase
+
+ class InaccessibleRecord < ActiveRecord::Base
+ attr_accessor :name
+
+ def mass_assign(attributes)
+ remove_attributes_protected_from_mass_assignment(attributes)
+ end
+ end
+
+ class AccessibleRecord < ActiveRecord::Base
+ disable_mass_assignment = false
+ end
+
+ # Replace this with your real tests.
+ def test_attr_protected_should_be_unusable_by_subclasses
+ InaccessibleRecord.send(:attr_protected, :name)
+ end
+
+ def test_should_not_allow_any_attributes_to_be_mass_assigned_by_default
+ accessible_attributes = mass_assign_attributes_to_inaccessible_record(:name => 'Batman')
+
+ assert !accessible_attributes.include?('Batman')
+ end
+
+ def test_attributes_should_be_mass_assignable_after_being_specified_with_attr_accessible
+ InaccessibleRecord.attr_accessible :name
+ accessible_attributes = mass_assign_attributes_to_inaccessible_record(:name => 'Batman')
+
+ assert accessible_attributes.include?('Batman')
+ ensure
+ InaccessibleRecord.accessible_attributes.delete(:name)
+ end
+
+ def test_should_log_additional_message_when_removed_from_mass_assignment
+ setup_testable_logger
+ mass_assign_attributes_to_inaccessible_record(:name => 'Batman')
+
+ assert_match /Batman/, log.string
+ ensure
+ revert_to_original_logger
+ end
+
+ def test_should_not_log_additional_message_when_not_removed_from_mass_assignment
+ setup_testable_logger
+ InaccessibleRecord.attr_accessible :name
+ mass_assign_attributes_to_inaccessible_record(:name => 'Batman')
+
+ assert_no_match /Batman/, log.string
+ ensure
+ revert_to_original_logger
+ InaccessibleRecord.accessible_attributes.delete(:name)
+ end
+
+ protected
+ def mass_assign_attributes_to_inaccessible_record(attributes)
+ inaccessible_record = InaccessibleRecord.new
+ inaccessible_record.mass_assign(attributes)
+ end
+
+ def setup_testable_logger
+ @original_logger = ActiveRecord::Base.logger
+ log = StringIO.new
+ ActiveRecord::Base.logger = Logger.new(log)
+ end
+
+ def revert_to_original_logger
+ ActiveRecord::Base.logger = @original_logger
+ end
+
+end
View
1 uninstall.rb
@@ -0,0 +1 @@
+# Uninstall hook code here

0 comments on commit ffc468b

Please sign in to comment.
Something went wrong with that request. Please try again.