From b1263a113ab8499ba647842994bbc525c2661ad5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Duque?= Date: Mon, 13 May 2024 18:22:07 -0500 Subject: [PATCH] Sort cars by multiplier & add author's field (#93) --- app/controllers/cars_controller.rb | 16 ++++++++-------- app/models/car.rb | 2 ++ app/services/csv_import_cars_service.rb | 3 ++- app/views/cars/_car.json.jbuilder | 2 +- app/views/cars/edit.haml | 3 +++ app/views/cars/show.haml | 4 ++++ config/locales/en.yml | 1 + 7 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/controllers/cars_controller.rb b/app/controllers/cars_controller.rb index da29eeeb..172d0a53 100644 --- a/app/controllers/cars_controller.rb +++ b/app/controllers/cars_controller.rb @@ -19,7 +19,7 @@ def index # GET /cars/rookie or /cars/rookie.json def rookie @cars = Rails.cache.fetch('rookie_cars', :expires_in => 1.month) do - @cars = cars_of_category(SYS::CATEGORY::ROOKIE) + @cars = cars_of_category(SYS::CATEGORY::ROOKIE).sort_by { |car| [car.multiplier, car.stock? ? 0 : 1] } end respond_with @cars do |format| @@ -30,7 +30,7 @@ def rookie # GET /cars/amateur or /cars/amateur.json def amateur @cars = Rails.cache.fetch('amateur_cars', :expires_in => 1.month) do - @cars = cars_of_category(SYS::CATEGORY::AMATEUR) + @cars = cars_of_category(SYS::CATEGORY::AMATEUR).sort_by { |car| [car.multiplier, car.stock? ? 0 : 1] } end respond_with @cars do |format| @@ -41,7 +41,7 @@ def amateur # GET /cars/advanced or /cars/advanced.json def advanced @cars = Rails.cache.fetch('advanced_cars', :expires_in => 1.month) do - @cars = cars_of_category(SYS::CATEGORY::ADVANCED) + @cars = cars_of_category(SYS::CATEGORY::ADVANCED).sort_by { |car| [car.multiplier, car.stock? ? 0 : 1] } end respond_with @cars do |format| @@ -52,7 +52,7 @@ def advanced # GET /cars/semipro or /cars/semipro.json def semipro @cars = Rails.cache.fetch('semipro_cars', :expires_in => 1.month) do - @cars = cars_of_category(SYS::CATEGORY::SEMI_PRO) + @cars = cars_of_category(SYS::CATEGORY::SEMI_PRO).sort_by { |car| [car.multiplier, car.stock? ? 0 : 1] } end respond_with @cars do |format| @@ -63,7 +63,7 @@ def semipro # GET /cars/pro or /cars/pro.json def pro @cars = Rails.cache.fetch('pro_cars', :expires_in => 1.month) do - @cars = cars_of_category(SYS::CATEGORY::PRO) + @cars = cars_of_category(SYS::CATEGORY::PRO).sort_by { |car| [car.multiplier, car.stock? ? 0 : 1] } end respond_with @cars do |format| @@ -74,7 +74,7 @@ def pro # GET /cars/superpro or /cars/superpro.json def superpro @cars = Rails.cache.fetch('superpro_cars', :expires_in => 1.month) do - @cars = cars_of_category(SYS::CATEGORY::SUPER_PRO) + @cars = cars_of_category(SYS::CATEGORY::SUPER_PRO).sort_by { |car| [car.multiplier, car.stock? ? 0 : 1] } end respond_with @cars do |format| @@ -85,7 +85,7 @@ def superpro # GET /cars/clockwork or /cars/clockwork.json def clockwork @cars = Rails.cache.fetch('clockwork_cars', :expires_in => 1.month) do - @cars = cars_of_category(SYS::CATEGORY::CLOCKWORK) + @cars = cars_of_category(SYS::CATEGORY::CLOCKWORK).sort_by { |car| [car.multiplier, car.stock? ? 0 : 1] } end respond_with @cars do |format| @@ -211,6 +211,6 @@ def set_car # Only allow a list of trusted parameters through. def car_params - params.require(:car).permit(:name, :speed, :accel, :weight, :multiplier, :folder_name, :category, :stock, :season) + params.require(:car).permit(:name, :speed, :accel, :weight, :multiplier, :folder_name, :category, :author, :stock, :season) end end diff --git a/app/models/car.rb b/app/models/car.rb index b36f131a..e38628b3 100644 --- a/app/models/car.rb +++ b/app/models/car.rb @@ -13,6 +13,7 @@ class Car field :multiplier, :type => Float field :folder_name, :type => String field :category, :type => Integer + field :author, :type => String field :stock, :type => Boolean, :default => false validates_presence_of :name @@ -22,6 +23,7 @@ class Car validates_presence_of :multiplier validates_presence_of :folder_name validates_presence_of :category + validates_presence_of :author validates_presence_of :stock def thumbnail_url diff --git a/app/services/csv_import_cars_service.rb b/app/services/csv_import_cars_service.rb index 5d256e44..336cc5f8 100644 --- a/app/services/csv_import_cars_service.rb +++ b/app/services/csv_import_cars_service.rb @@ -34,7 +34,8 @@ def call :weight => row[3], :multiplier => row[4], :folder_name => row[5], - :stock => true?(row[6]) + :author => row[6], + :stock => true?(row[7]) } cars << Car.new(car_hash) diff --git a/app/views/cars/_car.json.jbuilder b/app/views/cars/_car.json.jbuilder index 9000197b..42d481bb 100644 --- a/app/views/cars/_car.json.jbuilder +++ b/app/views/cars/_car.json.jbuilder @@ -1,3 +1,3 @@ -json.extract! car, :id, :name, :speed, :accel, :weight, :multiplier, :category, :stock, :folder_name, :created_at, +json.extract! car, :id, :name, :speed, :accel, :weight, :multiplier, :category, :author, :stock, :folder_name, :created_at, :updated_at json.url car_url(car, :format => :json) diff --git a/app/views/cars/edit.haml b/app/views/cars/edit.haml index 798b84f7..3d17e7dc 100644 --- a/app/views/cars/edit.haml +++ b/app/views/cars/edit.haml @@ -32,6 +32,9 @@ .field.form-group = f.label :folder_name, t("rva.cars.edit.folder-name") = f.text_field :folder_name, :class => "form-control" + .field-form-group + = f.label :author, t("rva.cars.features.author") + = f.text_field :author, :class => "form-control" .field.form-group = f.label :stock, t("rva.cars.features.stock") = f.check_box :stock diff --git a/app/views/cars/show.haml b/app/views/cars/show.haml index b748983b..8f397115 100644 --- a/app/views/cars/show.haml +++ b/app/views/cars/show.haml @@ -37,6 +37,10 @@ = t("rva.cars.features.category") %dd = category_name(@car.category) + %dt + = t("rva.cars.features.author") + %dd + = @car.author %dt{:style => "cursor: help;", :"data-toggle" => "tooltip", :"data-placement" => "top", :title => t("rva.cars.features.stock-tooltip.tooltip")} = t("rva.shared.stock.title") %dd diff --git a/config/locales/en.yml b/config/locales/en.yml index 3098ce7c..7dee8c98 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -425,6 +425,7 @@ en: weight: "Weight" multiplier: "Multiplier" category: "Category" + author: "Author" stock: "Stock" stock-tooltip: tooltip: "Original Re-Volt Content"