upmin / upmin-admin-ruby

Framework for creating powerful admin backends with minimal effort in Ruby on Rails.
MIT License
757 stars 66 forks source link

Upmin Model hijacking Class Name #171

Closed snarkitect closed 9 years ago

snarkitect commented 9 years ago

It looks like you're setting the upmin model class name to that of the ActiveRecord model. In our application, this doesn't seem to matter until we write tests. We can operate on our own models as needed and set read only attributes on the upmin AdminModels. This allows our application to dynamically update fields without opening them up for editing by some of our admin interface users.

When we run tests however, those tests run against the upmin models which prevent those same updates from succeeding.

Subscription.update(status: 'active') will work in a model or controller, but it won't work from a test if we've set attr_readonly(:status) on the AdminSubscription model.

jdurand commented 9 years ago

Upmin::Model automatically delegates everything to the associated Model.

If you want the field to appear in the form but be read only, I suggest you override the attribute partial. This is not well documented, but UpminAdmin tries a cascade of possible partial files for every attribute. I suggest you take a look at : Upmin::Railties::Render.

To log the possible partials you can override I've overridden this class in my app like so :

module Upmin::Railties
  module Render

    # Render method that is used by upmin-admin. Tries to render partials in order, passing data in as the :object, along with options.
    def up_render(data, options = {})
      if data.is_a?(Upmin::Model)
        options = RenderHelpers.model_options(data, options)
        partials = RenderHelpers.model_partials(data, options)

      elsif data.is_a?(Upmin::Attribute)
        options = RenderHelpers.attribute_options(data, options)
        partials = RenderHelpers.attribute_partials(data, options)

      elsif data.is_a?(Upmin::Association)
        options = RenderHelpers.association_options(data, options)
        partials = RenderHelpers.association_partials(data, options)

      elsif data.is_a?(Upmin::Action)
        options = RenderHelpers.action_options(data, options)
        partials = RenderHelpers.action_partials(data, options)

      elsif data.is_a?(Upmin::Parameter)
        options = RenderHelpers.parameter_options(data, options)
        partials = RenderHelpers.parameter_partials(data, options)

      elsif data.is_a?(Upmin::Query)
        options = RenderHelpers.search_results_options(data, options)
        partials = RenderHelpers.search_results_partials(data, options)

      elsif data.superclass == Upmin::Model
        # Probably rendering a search box
        options = RenderHelpers.search_box_options(data, options)
        partials = RenderHelpers.search_box_partials(data, options)

      else
        raise Upmin::ArgumentError.new(data)
      end

      # Log possible partials
      Rails.logger.info "  Upmin partials lookup: #{partials}"

      # Use options as the render hash, and set :object as the data being used for rendering.
      options[:object] = data

      partials.each do |partial|
        begin
          options[:partial] = partial
          return render(options)
        rescue ActionView::MissingTemplate => e
        end
      end

      # If we get here we tried all of the partials and nothing matched. This *shouldn't* be possible but might happen if partials are deleted.
      raise Upmin::MissingPartial.new(data)
    end

  end
end

On the other hand if you just want to remove that field from the form, I just pushed a little fix on the master branch to make it possible to specify different attributes for the create/edit form :

attributes :id, :name, :some_field
form_attributes :id, :name

I hope this can help you.