Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 2 additions & 6 deletions app/assets/config/manifest.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
//= link_tree ../images
//= link_directory ../stylesheets .css
//= link application.js
//= link controllers/application.js
//= link controllers/form_validation_controller.js
//= link controllers/nested_form_controller.js
//= link controllers/questions_controller.js
//= link controllers/index.js
//= link_tree ../../javascript .js
//= link_tree ../../../vendor/javascript .js
6 changes: 0 additions & 6 deletions app/assets/javascripts/application.js

This file was deleted.

3 changes: 3 additions & 0 deletions app/assets/stylesheets/global.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
.hide {
display: none;
}
.category-title-badge {
cursor: pointer;
}
55 changes: 55 additions & 0 deletions app/controllers/admin/badges_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

# app/controllers/admin/badges_controller.rb
module Admin
class BadgesController < ApplicationController
before_action :set_badge, only: %i[show edit update destroy]
before_action :get_images, only: %i[new edit]

def index
@badges = Badge.all
end

def new
@badge = Badge.new
end

def create
@badge = Badge.new(badge_params)
if @badge.save
redirect_to admin_badges_path, notice: t('admin.badges.create.success')
else
render :new
end
end

def edit; end

def update
if @badge.update(badge_params)
redirect_to admin_badges_path, notice: t('admin.badges.update.updated')
else
render :edit
end
end

def destroy
@badge.destroy
redirect_to admin_badges_path, alert: t('admin.badges.destroy.deleted')
end

private

def get_images
@images = Dir.glob(Rails.root.join('public', 'badges', '*')).map { |path| File.basename(path) }
end

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

def badge_params
params.require(:badge).permit(:title, :image, :image_url, :rule, :value, :status)
end
end
end
1 change: 1 addition & 0 deletions app/controllers/admin/tests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def set_test
end

def test_params
params[:test][:status] = params[:test][:status].to_i if params[:test][:status].present?
params.require(:test).permit(:title, :level, :category_id, :status)
end

Expand Down
9 changes: 9 additions & 0 deletions app/controllers/profile_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

# app/controllers/profiles_controller.rb
class ProfileController < ApplicationController
def show
@user = current_user
@earned_badges = @user.badges.order(created_at: :desc)
end
end
7 changes: 7 additions & 0 deletions app/controllers/test_passages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ def result; end
def update
@test_passage.accept!(params[:answer_ids])

@test_passage.check_successful_completed!
if @test_passage.completed?

Choose a reason for hiding this comment

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

у тебя тут проверка завершения и внутри check_successful_completed. получается что ты два раза одно и тоже проверяешь - неоптимально. стоит из метода check_successful_completed убрать тогда проверку завершенности и переместить внутрь if

if @test_passage.successful?
flash[:notice] = t('test_passages.update.success')
BadgeAssigner.new(@test_passage).call
else
flash[:alert] = t('test_passages.update.failed')
end
TestsMailer.completed_test(@test_passage).deliver_now
redirect_to result_test_passage_path(@test_passage)
else
Expand Down
16 changes: 16 additions & 0 deletions app/helpers/admin/badges_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Admin
module BadgesHelper
def badge_value_text(badge)
case badge.rule
when 'all_category_tests_passed'
badge.value.to_s
when 'all_level_tests_passed'
t("tests.levels.#{badge.value}")
else
badge.value.to_s
end
end
end
end
1 change: 0 additions & 1 deletion app/javascript/application.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@hotwired/turbo-rails"
import "controllers"
16 changes: 16 additions & 0 deletions app/javascript/controllers/admin/badge_value_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// app/javascript/controllers/admin/badge_value_controller.js
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
static targets = ["rule", "valueField", "template"]

connect() {
this.switchValueField()
}

switchValueField() {
const rule = this.ruleTarget.value
const template = this.templateTargets.find(t => t.dataset.rule === rule) || this.templateTargets.find(t => t.dataset.rule === 'default')
this.valueFieldTarget.innerHTML = template.innerHTML
}
}
19 changes: 19 additions & 0 deletions app/javascript/controllers/admin/category_copy_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Controller } from "@hotwired/stimulus"

// Контроллер для копирования значения из badge в input
export default class extends Controller {
static targets = ["input"]
static values = {
param: String
}

copy(event) {
const clickedElement = event.currentTarget
const value = clickedElement.dataset.categoryCopyValueParam

if (this.hasInputTarget && value) {
this.inputTarget.value = value
this.inputTarget.dispatchEvent(new Event('input')) // важно для реактивности
}
}
}
16 changes: 16 additions & 0 deletions app/javascript/controllers/admin/image_preview_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
static targets = [ "select", "preview" ]
connect() {
}

updateImage() {
const selectedImage = this.selectTarget.value;
if (selectedImage) {
this.previewTarget.src = `/badges/${selectedImage}`;
} else {
this.previewTarget.src = "";
}
}
}
11 changes: 0 additions & 11 deletions app/javascript/controllers/application.js

This file was deleted.

35 changes: 0 additions & 35 deletions app/javascript/controllers/form_validation_controller.js

This file was deleted.

24 changes: 16 additions & 8 deletions app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
// Import and register all your controllers from the importmap under controllers/*
import { Application } from "@hotwired/stimulus"

import { application } from "controllers/application"
import ImagePreviewController from "./admin/image_preview_controller"
import BadgeValueController from "./admin/badge_value_controller"
import CategoryCopyController from "./admin/category_copy_controller"

// Eager load all controllers defined in the import map under controllers/**/*_controller
import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
eagerLoadControllersFrom("controllers", application)
const application = Application.start()

application.register("image-preview", ImagePreviewController)
application.register("badge-value", BadgeValueController)
application.register("category-copy", CategoryCopyController)


// Configure Stimulus development experience
application.debug = true
window.Stimulus = application

export { application }

// Lazy load controllers as they appear in the DOM (remember not to preload controllers in import map!)
// import { lazyLoadControllersFrom } from "@hotwired/stimulus-loading"
// lazyLoadControllersFrom("controllers", application)
20 changes: 0 additions & 20 deletions app/javascript/controllers/nested_form_controller.js

This file was deleted.

43 changes: 0 additions & 43 deletions app/javascript/controllers/questions_controller.js

This file was deleted.

25 changes: 25 additions & 0 deletions app/models/badge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: badges
#
# id :bigint not null, primary key
# image :string
# image_url :string
# rule :string
# status :integer default("inactive")
# title :string
# value :string
# created_at :datetime not null
# updated_at :datetime not null
#
class Badge < ApplicationRecord
enum status: { inactive: 0, active: 1 }

scope :active, -> { where(status: :active) }
scope :inactive, -> { where(status: :inactive) }

has_many :user_badges, dependent: :destroy
has_many :badges, through: :user_badges
end
8 changes: 7 additions & 1 deletion app/models/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Table name: tests
#
# id :bigint not null, primary key
# duration :integer default(0)
# level :integer default("medium"), not null
# status :integer default("draft")
# title :string not null
Expand Down Expand Up @@ -40,7 +41,12 @@ class Test < ApplicationRecord
scope :medium, -> { where(level: :medium) }
scope :hard, -> { where(level: :hard) }
scope :ready, -> { where(status: 1) }
scope :by_category, ->(category_title) { joins(:category).where(categories: { title: category_title }) }
scope :by_category, lambda { |category_title|

Choose a reason for hiding this comment

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

объясни логику этого метода?

1 - передали пустую строку - вернется пустой массив, зачем обработка return self unless category_title.present??
2 -

 sanitized_title = ActiveRecord::Base.sanitize_sql_like(category_title)
    joins(:category).where(Category.arel_table[:title].matches(sanitized_title))

какую проблему тут решаешь?

return self unless category_title.present?

sanitized_title = ActiveRecord::Base.sanitize_sql_like(category_title)
joins(:category).where(Category.arel_table[:title].matches(sanitized_title))
}

def self.titles_by_category(category_title)
by_category(category_title).pluck(:title)
Expand Down
Loading