candy_wrapper/README.md

1.5 KiB

CANDY WRAPPER

Use form objects with ease. Plugs into inherited_resources easily:

# app/models/database_object.rb
class DatabaseObject < Persistence::Base
  # ... persistence and relationships only ...
end
# app/controllers/database_objects_controller.rb

class DatabaseObjectsController < ApplicationController
  inherit_resources

  # for the parts of inherited_resources that actually persist models,
  # ensure that persistence takes placed within a CandyWrapper::ModelWrapper
  # form object. Those respond to save, assign_attributes, and update_attributes.
  wrap_in_form_object!
end
# app/form_objects/database_object_form_object.rb

class DatabaseObjectFormObject < CandyWrapper::ModelWrapper
  def complex_parameter=(database_object, parameter_value)
    # do complex formatting here, probably for nested objects
    # database_object will be saved by this point
  end

  # perform these actions before database_object is saved
  before_wrapped_save :process_first_parameter

  def process_first_parameter=(database_object, parameter_value)
    # use normal accessors to set properties on database_object, it will be
    # saved when all before_wrapped_saved methods are run
  end

  # there is no guarantee in the order that these will run, don't make them
  # depend on each other!
end

Also, plug it into ActiveAdmin super-easily, which is where I typicallty use it!

ActiveAdmin.register Model do
  wrap_in_form_object
end