Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upmin Model hijacking Class Name #171

Closed
snarkitect opened this issue Apr 3, 2015 · 1 comment
Closed

Upmin Model hijacking Class Name #171

snarkitect opened this issue Apr 3, 2015 · 1 comment

Comments

@snarkitect
Copy link

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
Copy link
Contributor

jdurand commented Apr 17, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants