-
Notifications
You must be signed in to change notification settings - Fork 0
added the functionality of receiving rewards for completed testing #21
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,3 +5,6 @@ | |
| .hide { | ||
| display: none; | ||
| } | ||
| .category-title-badge { | ||
| cursor: pointer; | ||
| } | ||
| 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 |
| 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 |
| 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 |
| 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" |
| 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 | ||
| } | ||
| } |
| 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')) // важно для реактивности | ||
| } | ||
| } | ||
| } |
| 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 = ""; | ||
| } | ||
| } | ||
| } |
This file was deleted.
This file was deleted.
| 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) |
This file was deleted.
This file was deleted.
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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| | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. объясни логику этого метода? 1 - передали пустую строку - вернется пустой массив, зачем обработка какую проблему тут решаешь? |
||
| 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) | ||
|
|
||
There was a problem hiding this comment.
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