【发布时间】:2015-03-20 08:39:12
【问题描述】:
如何从 Rails 引擎扩展控制器方法而不必复制整个内容?
尝试扩展https://github.com/radar/forem/blob/rails4/app/controllers/forem/forums_controller.rb -- app/decorators/controllers/forem/forums_controller_decorator.rb:
理想
Forem::ForumsController.class_eval do
def show
# A simple `include` here or something?
# New code goes here...
end
end
当前
Forem::ForumsController.class_eval do
def show
# Repeat ALL the code from:
# https://github.com/radar/forem/blob/rails4/app/controllers/forem/forums_controller.rb
authorize! :show, @forum
register_view
@topics = if forem_admin_or_moderator?(@forum)
@forum.topics
else
@forum.topics.visible.approved_or_pending_review_for(forem_user)
end
@topics = @topics.by_pinned_or_most_recent_post
# Kaminari allows to configure the method and param used
@topics = @topics.send(pagination_method, params[pagination_param]).per(Forem.per_page)
respond_to do |format|
format.html
format.atom { render :layout => false }
end
# New code goes here...
end
end
【问题讨论】:
标签: ruby-on-rails rails-engines