A long-lived project that still receives updates
Clone records using a method from model in ActiveScaffold
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

Duplicate Action for ActiveScaffold

Currently, this gem is compatible with ActiveScaffold >= 3.3.x gems and rails >= 4.0.
Use 1.0.x for rails >= 3.x versions.

Overview

This gem adds an action to clone records. By default will only set attributes and belongs_to associations. You must override initialize_dup in your model or define a method to clone the record and set in conf.duplicate.method.

Installation

You’ll need at least ActiveScaffold 3.3.x to use this, and rails 3.x

gem install active_scaffold_duplicate

Usage

Step 1

Override initialize_dup in the model to set some attributes or copy associations:

# app/models/bill.rb
class Bill < ActiveRecord::Base
  belongs_to :customer
  has_many :items
  
  def initialize_dup(other)
    super
    self.items = other.items.map(&:dup)
  end
end

Step 2

Add duplicate action.

class BillsController < ApplicationController
  active_scaffold do |conf|
    conf.actions << :duplicate
  end
end

Step 3

Change method to :get in link if you want to display create form instead of cloning record. Set position to :replace or :after for inline display, or enable page rendering.

class BillsController < ApplicationController
  active_scaffold do |conf|
    conf.duplicate.link.method = :get
    conf.duplicate.link.position = :after
    #conf.duplicate.link.page = true # for new page rendering
  end
end

Also you can change it globally.

class ApplicationController < ActionController::Base
  active_scaffold.set_defaults do |conf|
    conf.duplicate.link.method = :get
    conf.duplicate.link.position = :after
    #conf.duplicate.link.page = true # for new page rendering
  end
end

You can access to record which is being duplicated in your form overrides with @old_record variable.

If you use :post method, you can enable refresh_list to refresh the list instead of only adding new record at top, or set action_after_clone to open edit form for example:

  conf.duplicate.refresh_list = true
  conf.duplicate.action_after_clone = :edit

Before/after controller methods

It’s possible to define some methods to add some custom code which require access to session or request params:

  • For :post method, before_duplicate_save(record) and after_duplicate_save(record) can be defined, which will be called before and after saving the record respectively.
  • For :new method, before_duplicate_new(record) can be defined, which will be called before rendering.

Support

If you have issues installing the gem, search / post to the Active Scaffold forum or Create an issue

Contributing

Fork, hack, push, and request a pull:

http://github.com/activescaffold/active_scaffold_duplicate/

License

Released under the MIT license (included).

Author

Contact me:


Sergio Cambra – irb(main):001:0> ( ‘sergioATprogramatica._see_s’.gsub(‘see’, ‘e’).gsub(‘AT’, ‘@’) )