Нистратов Артём
anistratov@go-promo.ru
Routes
resources :snippets
rake routes
resource :snippet
root to: 'snippets#index'
namespace :my do
resources :snippets do
put :publish, on: :member
end
end
namespace :my do
resources :snippets do
member do
put :publish
end
end
end
namespace :my do
resources :snippets
end
resources :users
resources :users do
resources :snippets
end
resources :snippets,
only: [:index, :show]
end
Controllers
class SnippetsController < ApplicationController
end
app/controllers/snippets_controller.rb
class SnippetsController < ApplicationController
def index
@snippets = Snippet.order(:created_at).last(10)
end
end
gem 'will_paginate'
class Snippet < ActiveRecord::Base
enum visibility: [ :_public, :_private ]
...
end
rails g migration add_visibility_to_snippet visibility:integer
scope :with_public_access, -> {
where visibility: visibilities[:_public]
}
...
end
scope :for_main, -> { with_public_access.order :created_at }
...
end
Controllers
app/controllers/snippets_controller.rb
class SnippetsController < ApplicationController
def index
@snippets = Snippet.order(:created_at).last(10)
end
end
class SnippetsController < ApplicationController
def index
@snippets = Snippet.for_main.page params[:page]
end
end
def publish
Snippet.find(params[:id])._public!
end
end
class SnippetsController < ::SnippetsController
before_action :find_user
...
end
class SnippetsController < ::SnippetsController
before_action :find_user
...
private
def find_user
@user = User.find params[:user_id] if params.key? :user_id
end
end
class SnippetsController < ::SnippetsController
before_action :find_user
...
private
def find_user
@user = User.find params[:user_id] if params.key? :user_id
end
def snippets
if @user.nil?
Snippet
else
@user.snippets
end
end
end
class SnippetsController < ::SnippetsController
before_action :find_user
def index
@snippets = snippets.for_main.page params[:page]
end
def publish
snippets.find(params[:id])._public!
end
private
def find_user
@user = User.find params[:user_id] if params.key? :user_id
end
def snippets
if @user.nil?
Snippet
else
@user.snippets
end
end
end
app/controllers/my/snippets_controller.rb
module My
class SnippetsController < ::SnippetsController
private def find_user
@user = ... # current user
end
end
end
get 'about', to: 'home#about'
get 'about' => 'home#about'
post 'feedback/:category', to: 'feedbacks#add'
post 'feedback(/:category)' => 'feedbacks#add'
match 'preview' => 'snippets#show'
match 'preview' => 'snippets#show',
via: [:get, :post]
match 'preview' => 'snippets#show',
via: [:get, :post],
as: :snippet_preview