Skip to content
mbrookes edited this page Nov 24, 2014 · 5 revisions

If you want to customize your models, you will need to create an admin model. First, create the folders app/upmin/models in your rails application. Then for each model you want to customize, simple create an Admin<ModelName> class in a file named app/upmin/models/admin_<model_name>.rb.

Inside of your admin model you can access the model instance through the model variable, and by default undefined methods are delegated to the model. Below are a couple examples to get you started.

Admin Product

class AdminProduct < Upmin::Model
   # The attributes method overwrites default attributes shown for a model, and replaces them with the provided attributes. These don't all need to be editable, but as long as there is an :attr_name= method available upmin assumes that the attribute should be editable.
  attributes :name, :short_desc, :price, :manufacturer, :free_shipping

  # The items_per_page method overrides the number of items shown for a model from the default of 30.
  # (The global default can also be modified in the upmin config initializer.)
  items_per_page 15

  # The actions method overwrites all actions and uses the provided list of methods.
  actions :update_price

  # You can use custom methods inside of admin that are explicitly for admin pages. For example, you might want a method to update the price and automatically add a 10% markup.
  def update_price(raw_price)
    model.price = raw_price * 1.10 # our markup is 10%
    model.save!
  end


end

Admin Shipment

class AdminShipment < Upmin::Model
  # Singular methods like `action` and `attribute` don't overwrite existing values, they just append to the existing ones.
  attribute :status

  # Singular methods like `action` and `attribute` don't overwrite existing values, they just append to the existing ones.
  action :update_shipment
  action :pretend_to_work

  def status
    return "TestStatus"

    # We are working to get Widgets added, that would make sharing ways to render data easier. For example, you might want to use a progress bar widget, or a shipment tracking widget.
    # return Upmin::Widget::ProgressBar.new(model.status, model.tracking_states)
  end

  def pretend_to_work(tps_reports)
    puts tps_reports
    return "102301401"
  end

end

Why Separate Files?

A few people have asked why we are using separate files instead of adding this to the model like we initially did. The biggest reasons for this is to help make code more maintainable in a larger project, and to avoid polluting the models with Upmin Admin specific functions. With this approach the only method added to models is the upmin_model method, which returns the same model inside of it's appropriate Admin<Model> class.