Skip to content

Commit

Permalink
resolved conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
FaxriddinMaxmadiyorov committed Oct 11, 2023
2 parents addf04c + 83a48a1 commit 7970080
Show file tree
Hide file tree
Showing 16 changed files with 361 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ require:
- "rubocop-rspec"
- "rubocop-factory_bot"

inherit_gem:
action_policy: config/rubocop-rspec.yml

AllCops:
NewCops: enable
TargetRubyVersion: 3.2
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ gem "jbuilder"
gem "kaminari", "~> 1.2", ">= 1.2.2"
gem "puma", "~> 5.0"
gem "rails", "~> 7.0.7", ">= 7.0.7.2"
gem "rails-i18n", "~> 7.0"
gem "ransack", "~> 4.0.0"
gem "redis", "~> 4.0"
gem "slim-rails"
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ GEM
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
rails-i18n (7.0.8)
i18n (>= 0.7, < 2)
railties (>= 6.0.0, < 8)
railties (7.0.7.2)
actionpack (= 7.0.7.2)
activesupport (= 7.0.7.2)
Expand Down Expand Up @@ -394,6 +397,7 @@ DEPENDENCIES
kaminari (~> 1.2, >= 1.2.2)
puma (~> 5.0)
rails (~> 7.0.7, >= 7.0.7.2)
rails-i18n (~> 7.0)
ransack (~> 4.0.0)
redis (~> 4.0)
rspec (~> 3.12)
Expand Down
8 changes: 3 additions & 5 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?

rescue_from ActionPolicy::Unauthorized, with: :user_not_authorized
rescue_from ActionPolicy::Unauthorized do |_e|
redirect_to root_path
end

private

def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: %i[name avatar])
devise_parameter_sanitizer.permit(:account_update, keys: %i[name avatar])
end

def user_not_authorized
redirect_to root_path
end
end
9 changes: 7 additions & 2 deletions app/controllers/vacancies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ def show
end

def new
@vacancy = Vacancy.new
default_values = { title: current_user.name }

@vacancy = Vacancy.new(default_values)
end

def edit
@vacancy = Vacancy.find(params[:id])
authorize!(@vacancy)
end

def create
Expand All @@ -27,6 +30,7 @@ def create

def update
@vacancy = Vacancy.find(params[:id])
authorize!(@vacancy)

if @vacancy.update(vacancy_params)
redirect_to vacancy_path(@vacancy)
Expand All @@ -37,8 +41,9 @@ def update

def destroy
@vacancy = Vacancy.find(params[:id])
@vacancy.destroy
authorize!(@vacancy)

@vacancy.destroy
redirect_to vacancies_path
end

Expand Down
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# updated_at :datetime not null
# role :integer default("applicant")
# name :string
# locale :string default("en")
#
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
Expand All @@ -27,4 +28,6 @@ class User < ApplicationRecord
foreign_key: :recipient_id, dependent: :destroy, inverse_of: :recipient

enum :role, %i[applicant company moderator admin]

validates :locale, presence: true
end
25 changes: 12 additions & 13 deletions app/policies/application_policy.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# Base class for application policies
class ApplicationPolicy < ActionPolicy::Base
# Configure additional authorization contexts here
# (`user` is added by default).
#
# authorize :account, optional: true
#
# Read more about authorization context: https://actionpolicy.evilmartians.io/#/authorization_context
def allow_record
current_admin? || owner_record?
end

# Define shared methods useful for most policies.
# For example:
#
# def owner?
# record.user_id == user.id
# end
private

def current_admin?
user.admin?
end

def owner_record?
user.id == record.user_id
end
end
7 changes: 7 additions & 0 deletions app/policies/vacancy_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class VacancyPolicy < ApplicationPolicy
alias_rule :edit?, :destroy?, :update?, to: :manage?

def manage?
allowed_to?(:allow_record)
end
end
4 changes: 4 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ class Application < Rails::Application
# config.eager_load_paths << Rails.root.join("extras")

config.generators.test_framework = :rspec

# i18n
config.i18n.available_locales = %i[en ru]
config.i18n.default_locale = :en
end
end
Loading

0 comments on commit 7970080

Please sign in to comment.