method load_resource for controllers #10392

Closed
wants to merge 1 commit into
from

Projects

None yet

6 participants

Contributor

This elegant solution follows rails 4 restful controller tendencies,
makes nested resources easier to understand
and leaves room for one's preffered authorization gem or custom logic.

load_resource_rails

Thank you guys for the hard work on helping developers.

Contributor

I think it can be convenient when using STI. We only need to inheritance the controller and override the set_* method. For example, say that we have one model AirConditoiner inherit from model Product, then we can simply do this in the controller:

class AirConditionerController < ProductController
  private
  def set_product
    load_resource do
      for_action(:index)                          {@products = AirConditioner.all}
      for_action(:new)                            {@product = AirConditioner.new}
      for_action(:create)                         {@product = AirConditioner.new(product_params)}
      for_action(:show, :edit, :update, :destroy) {@product = AirConditioner.find(params[id])}
    end
  end
end

I think it adds a good amount of indirection in controllers, making a "load" method responsible for setting up stuff for other different methods based on the current action, but it seems a good fit for trying out in a gem, though.

Please feel free to discuss your idea further in the Rails Core mailing list, it's easier to get more feedback from there. Thank you!

Contributor

@tonytonyjan @carlosantoniodasilva yes, I've created a gem as well, if you're interested :) https://github.com/yakko/load_resource I promise to keep it up to date

I've started a discussion on the mailing list https://groups.google.com/forum/?fromgroups=#!topic/rubyonrails-core/HntOmspK_s8

@carlosantoniodasilva rails 4 latest scaffold controller template comes with half of the resources set up in a before_filter, after some atempts I came up with that solution.
Thanks for your review on it

Owner
dhh commented May 1, 2013

This seems like a good fit for a gem, but I don't like the aesthetic of it personally. It adds additional indirection that I don't think is a good fit for the default scaffold and I wouldn't personally use a setup like this in a real app either.

Contributor

@dhh 💔

Contributor

Neat! I'm for anything that reduces code in a clear, flexible, declarative way.

Have you looked at restful_json? Though it evolved as a desire to implement JSON service controllers quickly and easily, the idea is to allow any format, so the name needs to change, eventually. It just requires a module include and then it is a semi-DSL for controller implementation. Basically almost anything that doesn't require much controller tweaking for action methods should work, in theory. And for things that require controller mods, you can override only what you need to in the controller or monkey-patch it. No solution (except Love!) works for everything, but it might help.

I think it makes sense since a lot of gems are based on a before_filter that loads the resources internally and prevent customization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment