administrate icon indicating copy to clipboard operation
administrate copied to clipboard

view variants support

Open hksk opened this issue 1 year ago • 2 comments

  • What were you trying to do? add a view variant, example:
$ ls -lah app/views/admin/courses
drwxr-xr-x  7 casa  staff   224B May  4 02:17 .
drwxr-xr-x  4 casa  staff   128B May  4 01:00 ..
-rw-r--r--  1 casa  staff   2.0K May  4 02:38 show+student.html.erb
-rw-r--r--  1 casa  staff   2.0K May  4 02:38 show.html.erb
  • What did you end up with (logs, or, even better, example apps are great!)? currently in the main controller, cant pass a variant, seems variant: :value not works
# main controller
    def show
      render locals: {
        page: Administrate::Page::Show.new(dashboard, requested_resource)
      }
    end

so I implemented something like this in my generated controller

# generated controller
    def show 
      variant = ""
      if current_user.is_student
        variant = "+student"
      end
      page = Administrate::Page::Show
      render "show#{variant}", locals: {
          page: Administrate::Page::Show.new(dashboard, requested_resource)
      }
    end

I guess we can add some like

# main controller
    def show
     variant = ""
     if !request.variant.nil? 
       variant = "+#{request.variant.to_s}"
     end
      render render "show#{variant}"locals: {
        page: Administrate::Page::Show.new(dashboard, requested_resource)
      }
    end
# generated controller
    def show
      request.variant = :student
      super
    end

hksk avatar May 04 '24 07:05 hksk

I'm not familiar with view variants. Is this a Rails feature we're not supporting? If so, could you link to the docs so we can plan out how to support it?

nickcharlton avatar Sep 23 '24 14:09 nickcharlton

I'm not familiar with view variants. Is this a Rails feature we're not supporting? If so, could you link to the docs so we can plan out how to support it?

https://guides.rubyonrails.org/layouts_and_rendering.html#the-variants-option

Thrizian avatar Oct 14 '24 09:10 Thrizian

@Thrizian I didn’t know about variant —this was really helpful to learn!

@hksk I tried it out briefly, and it worked without needing any additional modifications. Here’s an example of how it’s used:

touch app/views/admin/courses/show.html+student.erb
module Admin
  class CoursesController < Admin::ApplicationController
    before_action :with_variant, only: %i[show]
    private def with_variant
      if SOME_CONDITION
        request.variant = :student
      end
    end

goosys avatar Oct 30 '24 10:10 goosys

Oh! I see, it's those!

@goosys, when you were looking into this, did you see a way which would make this easier to do? (Like an API administrate could support?)

nickcharlton avatar Dec 31 '24 17:12 nickcharlton

Hm, that all said, I think solving this with documentation is fairly reasonable too, if we had a section in the Guides, for example!

nickcharlton avatar Dec 31 '24 17:12 nickcharlton

@nickcharlton

No, the sample code I provided was the simplest approach. Since you only need to modify request.variant before Rails searches for the template, there are several possible ways and timings to achieve this. However, I believe it would be difficult for Administrate to support this directly, as the optimal solution would vary depending on the application. (It could be done using either before_action or after_action, and unifying the logic inside with_variant within Administrate would also be challenging.)

I also think that providing guidance in the documentation would be sufficient support.

goosys avatar Jan 16 '25 04:01 goosys

Yeah, that makes a lot of sense.

I'm quite keen to start documenting "advanced" usage of Administrate. We can do our best in our pitch to avoid straying from Rails defaults, but in practice, I find that I'd rather something akin to a cookbook which helps people solve their problems quicker.

Would be able to contribute your sample code and an explanation in a PR?

nickcharlton avatar Jan 16 '25 12:01 nickcharlton

@nickcharlton I have added a guid and some samples. Please review! https://github.com/thoughtbot/administrate/pull/2777

goosys avatar Feb 13 '25 07:02 goosys