Skip to content

Commit

Permalink
add subscription fields to users, only active subscribers can view pr…
Browse files Browse the repository at this point in the history
…emium posts
  • Loading branch information
yshmarov committed Mar 11, 2021
1 parent 40684e3 commit d49efa0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
50 changes: 19 additions & 31 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,68 +1,56 @@
class PostsController < ApplicationController
before_action :set_post, only: %i[ show edit update destroy ]

# GET /posts or /posts.json
def index
@posts = Post.all
# only active subscribers can see premium posts
if current_user.subscription_status == "active"
@posts = Post.all
else
@posts = Post.free
end
end

# GET /posts/1 or /posts/1.json
def show
# only active subscribers can see premium posts
if @post.premium? && current_user.subscription_status != "active"
redirect_to posts_path, alert: "#{@post.title} is available only for premium subscribers"
end
end

# GET /posts/new
def new
@post = Post.new
end

# GET /posts/1/edit
def edit
end

# POST /posts or /posts.json
def create
@post = Post.new(post_params)

respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: "Post was successfully created." }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
if @post.save
redirect_to @post, notice: "Post was successfully created."
else
render :new, status: :unprocessable_entity
end
end

# PATCH/PUT /posts/1 or /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: "Post was successfully updated." }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
if @post.update(post_params)
redirect_to @post, notice: "Post was successfully updated."
else
render :edit, status: :unprocessable_entity
end
end

# DELETE /posts/1 or /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: "Post was successfully destroyed." }
format.json { head :no_content }
end
redirect_to posts_url, notice: "Post was successfully destroyed."
end

private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end

# Only allow a list of trusted parameters through.
def post_params
params.require(:post).permit(:title, :content, :premium)
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Post < ApplicationRecord

validates :title, :content, presence: true

scope :free, -> { where(premium: false) }

def to_s
title
Expand Down
6 changes: 6 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
<% if current_user %>
<%= link_to current_user.email, edit_user_registration_path %>
<%= link_to "Log out", destroy_user_session_path, method: :delete %>
<br>
Plan:
<%= current_user.plan %>
<br>
Subscription status:
<%= current_user.subscription_status %>
<% else %>
<%= link_to "Log in", new_user_session_path %>
<%= link_to "Register", new_user_registration_path %>
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20210311212919_add_subscription_fields_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddSubscriptionFieldsToUsers < ActiveRecord::Migration[6.1]
def change
add_column :users, :plan, :string
add_column :users, :subscription_status, :string, default: "incomplete"
end
end
4 changes: 3 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d49efa0

Please sign in to comment.