Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E2347. Reimplement duties controller.rb and badges controller.rb #39

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ end
group :development do
# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
gem 'spring'
end
end
9 changes: 6 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ GEM
net-smtp (0.3.3)
net-protocol
nio4r (2.5.8)
nokogiri (1.14.2-x86_64-linux)
nokogiri (1.14.2-aarch64-linux)
racc (~> 1.4)
nokogiri (1.14.2-arm64-darwin)
racc (~> 1.4)
parallel (1.22.1)
parser (3.2.1.0)
Expand Down Expand Up @@ -213,7 +215,8 @@ GEM
zeitwerk (2.6.7)

PLATFORMS
x86_64-linux
aarch64-linux
sankettangade marked this conversation as resolved.
Show resolved Hide resolved
arm64-darwin-22

DEPENDENCIES
bcrypt (~> 3.1.7)
Expand All @@ -236,4 +239,4 @@ RUBY VERSION
ruby 3.2.1p31

BUNDLED WITH
2.4.7
2.4.12
76 changes: 76 additions & 0 deletions app/controllers/api/v1/badges_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
class Api::V1::BadgesController < ApplicationController

before_action :set_badge, only: [:show, :edit, :update, :destroy]
before_action :set_return_to, only: [:new]

def action_allowed?
unless current_user_has_ta_privileges?
redirect_to login_path, alert: "You don't have permission to access this page"
end
end

def index
@badges = Badge.all
sankettangade marked this conversation as resolved.
Show resolved Hide resolved
end

def show
@badge = Badge.find(params[:id])
render json: @badge
end

def new
sankettangade marked this conversation as resolved.
Show resolved Hide resolved
@badge = Badge.new
end

def create
@badge = Badge.new(badge_params)

if @badge.save
redirect_to redirect_to_url, notice: 'Badge was successfully created'
sankettangade marked this conversation as resolved.
Show resolved Hide resolved
else
render :new
sankettangade marked this conversation as resolved.
Show resolved Hide resolved
end
end

def update
if @badge.update(badge_params)
redirect_to api_v1_badge_url(@badge), notice: 'Badge was successfully updated.'
sankettangade marked this conversation as resolved.
Show resolved Hide resolved
else
render :edit
end
end

def destroy
@badge.destroy
redirect_to badges_url, notice: 'Badge was successfully destroyed.'
end





private
def set_badge
@badge = Badge.find(params[:id])
end

def badges_url
"/api/v1/badges"
end

def set_return_to
session[:return_to] ||= request.referer
end

def redirect_to_url
session.delete(:return_to) || badges_url
end

def badge_params
params.require(:badge).permit(:name, :description, :image_name, :image_file)
end

def redirect_to_url
session.delete(:return_to) || api_v1_badges_url
end
end
64 changes: 64 additions & 0 deletions app/controllers/api/v1/duties_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

class Api::V1::DutiesController < ApplicationController
before_action :set_duty, only: %i[show edit update destroy]

def index
@duties = Duty.all
end

def show; end

def new
@duty = Duty.new
@id = params[:id]
end

def edit; end

def create
@duty = Duty.new(duty_params)

if @duty.save
redirect_to redirect_to_url, notice: 'Role was successfully created.'
else
redirect_to_create_page_and_show_error
end
end

def update
if @duty.update(duty_params)
redirect_to redirect_to_url, notice: 'Role was successfully updated.'
else
redirect_to_create_page_and_show_error
end
end

def destroy
@duty.destroy
redirect_to redirect_to_url, notice: 'Role was successfully deleted.'
end

private

def set_duty
@duty = Duty.find(params[:id])
end

def redirect_to_edit_assignment_path
redirect_to edit_assignment_path(duty_params[:assignment_id])
end

def redirect_to_create_page_and_show_error
# flash[:error] = @duty.errors.full_messages.join('. ')
redirect_to action: :new, id: duty_params[:assignment_id]
end

def duty_params
params.require(:duty).permit(:assignment_id, :max_members_for_duty, :name)
end
end

def redirect_to_url
"/api/v1/duties"
end
20 changes: 20 additions & 0 deletions app/models/badge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Badge < ApplicationRecord

def self.get_id_from_name(badge_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please help me locate where we have utilized this class method and other methods in this model? I am having difficulty finding its application.

Badge.find_by(name: badge_name)&.id
end

def self.get_image_name_from_name(badge_name)
Badge.find_by(name: badge_name)&.image_name
end

def self.upload_image(image_file)
return '' unless image_file

image_name = image_file.original_filename
File.open(Rails.root.join('app', 'assets', 'images', 'badges', image_name), 'wb') do |file|
file.write(image_file.read)
end
image_name
end
end
20 changes: 20 additions & 0 deletions app/models/duty.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Duty < ApplicationRecord
belongs_to :assignment
validates :name, presence: true,
format: { with: /\A[^`!@#\$%\^&*+_=]+\z/,
message: 'Please enter a valid role name' },
length: {
minimum: 3,
message: 'Role name is too short (minimum is 3 characters)'
}
validates :name, uniqueness: { case_sensitive: false, scope: :assignment_id,
sankettangade marked this conversation as resolved.
Show resolved Hide resolved
message: 'The role "%{value}" is already present for this assignment' }
validates :max_members_for_duty, numericality: { only_integer: true, greater_than_or_equal_to: 1,
message: 'Value for max members for role is invalid' }



def can_be_assigned?(team)
max_members_for_duty > team.participants.select { |team_member| team_member.duty_id == id }.count
end
end
47 changes: 42 additions & 5 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,55 @@
# MySQL. Versions 5.5.8 and up are supported.
sankettangade marked this conversation as resolved.
Show resolved Hide resolved
#
# Install the MySQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem "mysql2"
#
# And be sure to use new-style password hashing:
# https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html
#
default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
port: 3306
socket: /var/run/mysqld/mysqld.sock
username: dev
password: root


development:
<<: *default
url: <%= ENV['DATABASE_URL'].gsub('?', '_development?') %>
database: reimplementation_development

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
url: <%= ENV['DATABASE_URL'].gsub('?', '_test?') %>
database: reimplementation_test

# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
# DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
# production:
# url: <%= ENV["MY_APP_DATABASE_URL"] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
<<: *default
url: <%= ENV['DATABASE_URL'].gsub('?', '_production?') %>
database: reimplementation_production
username: reimplementation
password: <%= ENV["REIMPLEMENTATION_DATABASE_PASSWORD"] %>
55 changes: 0 additions & 55 deletions config/database.yml.example

This file was deleted.

5 changes: 2 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
Rails.application.routes.draw do
mount Rswag::Ui::Engine => '/api-docs'
mount Rswag::Api::Engine => '/api-docs'
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Defines the root path route ("/")
# root "articles#index"
namespace :api do
namespace :v1 do
resources :badges
resources :duties
resources :institutions
resources :roles
resources :users do
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20230423205355_create_badges.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateBadges < ActiveRecord::Migration[7.0]
def change
create_table :badges do |t|
t.string :name
t.string :description
t.string :image_name

t.timestamps
end
end
end
11 changes: 11 additions & 0 deletions db/migrate/20230423220953_create_duties.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateDuties < ActiveRecord::Migration[7.0]
def change
create_table :duties do |t|
t.string :name
t.integer :max_members_for_duty
t.references :assignment, null: false, foreign_key: true

t.timestamps
end
end
end
Loading