Skip to content
Browse files

#1731 incorporating patch submitted by dpmccabe for strong_parameters…

… as optional DSL method
  • Loading branch information...
1 parent c72adef commit 0ba1fb34071c0a0295e4666192c18edd6e00f61e @garysweaver committed
Showing with 56 additions and 0 deletions.
  1. +4 −0 README.rdoc
  2. +8 −0 lib/active_admin/dsl.rb
  3. +44 −0 lib/active_admin/strong_parameters_patch.rb
View
4 README.rdoc
@@ -82,6 +82,10 @@ If you get:
uninitialized constant Admin::DashboardController
when trying to view the dashboard (at +/admin+), ensure +app/admin/dashboards.rb+ looks like the current {default}[https://raw.github.com/gregbell/active_admin/master/lib/generators/active_admin/install/templates/dashboard.rb].
+== Strong Parameters
+
+If you are an early adopter of strong_parameters which will be part in Rails 4, add `support_strong_parameters` to your controller definition.
+
== Next Steps
View
8 lib/active_admin/dsl.rb
@@ -1,3 +1,5 @@
+require 'active_admin/strong_parameters_patch'
+
module ActiveAdmin
#
@@ -11,6 +13,12 @@ def initialize(config)
@config = config
end
+ #TODO: replace this temporary hack for strong_parameters, part of Rails 4.0 support
+ # include patch provided by dpmccabe in https://gist.github.com/3718571
+ def support_strong_parameters
+ @config.controller.class_eval("include ActiveAdmin::StrongParametersPatch")
+ end
+
# Runs the registration block inside this object
def run_registration_block(&block)
instance_eval &block if block_given?
View
44 lib/active_admin/strong_parameters_patch.rb
@@ -0,0 +1,44 @@
+module ActiveAdmin
+ # The following contains code provided by dpmccabe in https://gist.github.com/3718571
+ module StrongParametersPatch
+ extend ActiveSupport::Concern
+
+ def initialize
+ @instance_name = active_admin_config.resource_name.downcase
+ @klass = active_admin_config.resource_name.constantize
+
+ @column_names = @klass.columns.map do |column|
+ unless [:id, :created_at, :updated_at].include?(column.name.to_sym)
+ case column.type
+ when :datetime, :date, :time
+ ([column.name.to_sym] + (1..5).inject([]) { |acc, x| acc << :"#{column.name}(#{x}i)" })
+ else
+ column.name.to_sym
+ end
+ end
+ end.flatten
+
+ super
+ end
+
+ def create
+ resource_obj = instance_variable_set("@#{@instance_name}", @klass.new(params[@instance_name.to_sym].permit(*@column_names)))
+
+ if resource_obj.save
+ redirect_to send("admin_#{@instance_name}_path", resource_obj), notice: "Created #{@instance_name}."
+ else
+ render :new
+ end
+ end
+
+ def update
+ resource_obj = instance_variable_set("@#{@instance_name}", @klass.find(params[:id]))
+
+ if resource_obj.update_attributes(params[@instance_name.to_sym].permit(*@column_names))
+ redirect_to send("admin_#{@instance_name}_path", resource_obj), notice: "Updated #{@instance_name}."
+ else
+ render :edit
+ end
+ end
+ end
+end

0 comments on commit 0ba1fb3

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