Created
October 12, 2017 12:08
-
-
Save fedorkk/9b63828febd75708dd744cd100d7d151 to your computer and use it in GitHub Desktop.
resourseable_concern.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Resourceable | |
extend ActiveSupport::Concern | |
included do | |
helper_method :resource, :resources, :resource_class | |
end | |
def index; end | |
def show; end | |
def edit | |
render 'new' | |
end | |
def new | |
@resource = build_resource | |
end | |
def create | |
@resource = build_resource(resource_params) | |
respond_to do |format| | |
if resource.save | |
format.html { redirect_to action: :index, notice: t('.created') } | |
else | |
format.html { render :new } | |
end | |
end | |
end | |
def update | |
respond_to do |format| | |
if resource.update(resource_params) | |
format.html { redirect_to action: :show, notice: t('.updated') } | |
else | |
format.html { render :new } | |
end | |
end | |
end | |
def destroy | |
resource.destroy | |
respond_to do |format| | |
format.html { redirect_to action: 'index', notice: t('.deleted') } | |
end | |
end | |
private | |
def resource | |
@resource ||= resource_class.find(params[:id]) | |
end | |
def resources | |
@resources ||= resource_class.all | |
end | |
def resource_class | |
raise 'Should be implemented localy' | |
end | |
def resource_params | |
raise 'Should be implemented localy' | |
end | |
end | |
# Использование: | |
class ArticlesController < ApplicaitonController | |
include Resourceable | |
private | |
def resource_class | |
Article | |
end | |
def resource_params | |
params.require(:article).permit( | |
:title, :text, :theme_id | |
) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment