Skip to content

Commit

Permalink
Remove default comments
Browse files Browse the repository at this point in the history
Rename delete cache method
  • Loading branch information
dngst committed Feb 19, 2024
1 parent 925bb9f commit 2b48f54
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 67 deletions.
14 changes: 4 additions & 10 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,24 @@ class ArticlesController < ApplicationController
before_action :set_article, only: %i[show edit update destroy]
before_action :require_admin, only: %i[new create edit update destroy]

# GET /articles or /articles.json
def index
@pagy, @articles = pagy(fetch_articles_for_current_user, items: 20)
end

# GET /articles/1 or /articles/1.json
def show
@article.mark_as_viewed_by_user(current_user) if user_signed_in?
end

# GET /articles/new
def new
@article = Article.new
end

# GET /articles/1/edit
def edit; end

# POST /articles or /articles.json
def create
@article = Article.new(article_params)
if @article.save
delete_article_ids_cache
clear_cache
redirect_to article_url(@article), notice: t('articles.saved')
else
render :new, status: :unprocessable_entity
Expand All @@ -39,17 +34,16 @@ def create

def update
if @article.update(article_params)
delete_article_ids_cache
clear_cache
redirect_to article_url(@article), notice: t('articles.updated')
else
render :edit, status: :unprocessable_entity
end
end

# DELETE /articles/1 or /articles/1.json
def destroy
@article.destroy
delete_article_ids_cache
clear_cache
redirect_to articles_url, notice: t('articles.deleted')
end

Expand Down Expand Up @@ -77,7 +71,7 @@ def article_params
params.require(:article).permit(:title, :body, :user_id, :published, :property_id)
end

def delete_article_ids_cache
def clear_cache
Rails.cache.delete('article_ids')
end
end
46 changes: 17 additions & 29 deletions app/controllers/properties_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,73 +7,61 @@ class PropertiesController < ApplicationController
before_action :require_admin
before_action :set_property, only: %i[show edit update destroy]

# GET /properties or /properties.json
def index
ids = Rails.cache.fetch('property_ids') do
Property.pluck(:id)
end
@pagy, @properties = pagy(Property.where(id: ids, user_id: current_user.id).order(created_at: :desc))
@pagy, @properties = pagy(Property.where(id: property_ids, user_id: current_user.id).order(created_at: :desc))
end

# GET /properties/1 or /properties/1.json
def show; end

# GET /properties/new
def new
@property = Property.new
end

# GET /properties/1/edit
def edit; end

# POST /properties or /properties.json
def create
@property = Property.new(property_params)

respond_to do |format|
if @property.save
delete_property_ids_cache
format.html { redirect_to property_url(@property), notice: t('properties.saved') }
else
format.html { render :new, status: :unprocessable_entity }
end
if @property.save
clear_cache
redirect_to property_url(@property), notice: t('properties.saved')
else
render :new, status: :unprocessable_entity
end
end

# PATCH/PUT /properties/1 or /properties/1.json
def update
if @property.update(property_params)
delete_property_ids_cache
respond_to do |format|
format.html { redirect_to property_url(@property), notice: t('properties.updated') }
end
clear_cache
redirect_to property_url(@property), notice: t('properties.updated')
else
render :edit, status: :unprocessable_entity
end
end

# DELETE /properties/1 or /properties/1.json
def destroy
@property.destroy
delete_property_ids_cache
respond_to do |format|
format.html { redirect_to properties_url, notice: t('properties.deleted') }
end
clear_cache
redirect_to properties_url, notice: t('properties.deleted')
end

private

# Use callbacks to share common setup or constraints between actions.
def set_property
@property = Property.friendly.find(params[:id])
end

# Only allow a list of trusted parameters through.
def property_params
params.require(:property).permit(:name, :user_id)
end

def delete_property_ids_cache
def clear_cache
Rails.cache.delete('property_ids')
end

def property_ids
Rails.cache.fetch('property_ids') do
Property.pluck(:id)
end
end
end
36 changes: 14 additions & 22 deletions app/controllers/requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,53 @@ class RequestsController < ApplicationController
before_action :set_user
before_action :set_request, only: %i[show edit update destroy close_request reopen_request]

# GET /requests or /requests.json
def index
ids = Rails.cache.fetch('request_ids') do
Request.pluck(:id)
end

items_per_page = 20

@request_user = User.friendly.find(params[:user_id])
@requests = if current_user&.admin?
admin_requests_query(ids).includes([:user]).order(created_at: :desc)
admin_requests_query(request_ids).includes([:user]).order(created_at: :desc)
else
user_requests_query(ids).order(created_at: :desc)
user_requests_query(request_ids).order(created_at: :desc)
end

@pagy, @requests = pagy(@requests, items: items_per_page)
@pagy, @requests = pagy(@requests, items: 20)
end

# GET /requests/1 or /requests/1.json
def show
@user = @request.user
@comment_count ||= @request.comments.count
@comment = @request.comments.build(user: @user)
end

# GET /requests/new
def new
@request = Request.new
end

# GET /requests/1/edit
def edit; end

# POST /requests or /requests.json
def create
@request = @user.requests.new(request_params)

if @request.save
delete_request_ids_cache
clear_cache
NewRequestMailer.request_notification(User.find(@request.user.admin_id), @request).deliver_later
redirect_to user_request_url(@user, @request), notice: t('requests.saved')
else
render :new, status: :unprocessable_entity
end
end

# PATCH/PUT /requests/1 or /requests/1.json
def update
@request = @user.requests.friendly.find(params[:id])
if @request.update(request_params)
delete_request_ids_cache
clear_cache
redirect_to user_request_url, notice: t('requests.updated')
else
render :edit, status: :unprocessable_entity
end
end

# DELETE /requests/1 or /requests/1.json
def destroy
@request.destroy
delete_request_ids_cache
clear_cache
redirect_to user_requests_url, notice: t('requests.deleted')
end

Expand All @@ -89,7 +75,7 @@ def reopen_request
flash[:alert] = t('requests.failed_to_reopen')
render :show
end
delete_request_ids_cache
clear_cache
end

private
Expand All @@ -114,7 +100,7 @@ def request_params
params.require(:request).permit(:title, :description, :user_id)
end

def delete_request_ids_cache
def clear_cache
Rails.cache.delete('request_ids')
end

Expand All @@ -127,4 +113,10 @@ def admin_requests_query(ids)
def user_requests_query(ids)
Request.where(id: ids, user_id: current_user.id)
end

def request_ids
Rails.cache.fetch('request_ids') do
Request.pluck(:id)
end
end
end
15 changes: 9 additions & 6 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ class UsersController < ApplicationController
before_action :handle_customer_details, only: [:show]

def index
ids = Rails.cache.fetch('tenant_ids') do
User.pluck(:id)
end
tenants_list = User.where(id: ids, admin: false, admin_id: current_user.id).order(created_at: :desc)
tenants_list = User.where(id: user_ids, admin: false, admin_id: current_user.id).order(created_at: :desc)
@pagy, @users = pagy(tenants_list)
end

Expand All @@ -34,7 +31,7 @@ def update

def destroy
@user.destroy
delete_tenant_ids_cache
clear_cache
redirect_to users_path, notice: t('users.deleted')
end

Expand Down Expand Up @@ -74,7 +71,13 @@ def tenant_params
params.require(:tenant).permit(:amount_due, :moved_in, :next_payment, :unit_number, :unit_type, :property_id)
end

def delete_tenant_ids_cache
def clear_cache
Rails.cache.delete('tenant_ids')
end

def user_ids
Rails.cache.fetch('tenant_ids') do
User.pluck(:id)
end
end
end

0 comments on commit 2b48f54

Please sign in to comment.