Publishing Module [PRE-GEM-CONCEPT]
hackerbrief# Publishing
Publishing Module for Ruby on Rails Model’s.
Location of Module
~/app/models/concerns/publishing.rb
Migration to Model
$ rails g migration AddPublishingTo{MODEL} published:boolean published_at:datetime:index
Run Migration to Database
$ rails db:migrate
Model
include Publishing
Controller Params
params.require(:model).permit(:published, :published_at)
Queries
# All Models where published: true
Model.is_published
# All Models where published: false
Model.is_unpublished
Ordering
# published_at: :asc
Model.is_published.published_asc
# published_at: :desc
Model.is_published.published_desc
Controller Action
def publish
@model = Model.find(params[:id])
@model.publish_toggle
# Response
end
View
= link_to @model.publish_link, model_publish_path(@model), method: :put
Routes
put '/model/:id/publish' => 'model#publish'
put '/model/:id/publish', to: 'model#publish'
put '/model/:id/publish', to: :publish, controller: 'models'