From 697c893b1731b92ef1469adcbf5044c83458f8fe Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Mon, 9 Nov 2015 12:11:53 -0500 Subject: [PATCH] Add Cost Stat --- app/controllers/admin/cards_controller.rb | 1 + app/controllers/api/v1/cards_controller.rb | 2 +- app/models/card.rb | 3 +++ app/serializers/api/v1/card_serializer.rb | 6 +++++- app/views/admin/cards/_form.html.slim | 13 +++++++++++++ .../20151109165630_add_cost_stat_to_card.rb | 15 +++++++++++++++ db/schema.rb | 3 ++- db/seeds.rb | 1 + 8 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20151109165630_add_cost_stat_to_card.rb diff --git a/app/controllers/admin/cards_controller.rb b/app/controllers/admin/cards_controller.rb index b13d993..15dc935 100644 --- a/app/controllers/admin/cards_controller.rb +++ b/app/controllers/admin/cards_controller.rb @@ -17,6 +17,7 @@ class Admin::CardsController < Admin::BaseController @card.build_hp_stat @card.build_atk_stat @card.build_def_stat + @card.build_cost_stat authorize @card end diff --git a/app/controllers/api/v1/cards_controller.rb b/app/controllers/api/v1/cards_controller.rb index 8fc255e..707c213 100644 --- a/app/controllers/api/v1/cards_controller.rb +++ b/app/controllers/api/v1/cards_controller.rb @@ -4,7 +4,7 @@ class Api::V1::CardsController < Api::V1::BaseController @cards = Card.includes(:character, :rarity, :type, :leader_skill, :passive_skill, :super_attack, :awaken_type, :dokkan_card, :hp_stat, :atk_stat, :def_stat, - :links).all.order(:gameid) + :cost_stat, :links).all.order(:gameid) render json: @cards, root: false end diff --git a/app/models/card.rb b/app/models/card.rb index 2e4cdcf..7c8c608 100644 --- a/app/models/card.rb +++ b/app/models/card.rb @@ -19,10 +19,12 @@ class Card < ActiveRecord::Base belongs_to :hp_stat, class_name: 'Stat' belongs_to :atk_stat, class_name: 'Stat' belongs_to :def_stat, class_name: 'Stat' + belongs_to :cost_stat, class_name: 'Stat' accepts_nested_attributes_for :hp_stat accepts_nested_attributes_for :atk_stat accepts_nested_attributes_for :def_stat + accepts_nested_attributes_for :cost_stat delegate :name, to: :character, prefix: false @@ -59,6 +61,7 @@ class Card < ActiveRecord::Base self.hp_stat.stat_type_id = 1 self.atk_stat.stat_type_id = 2 self.def_stat.stat_type_id = 3 + self.cost_stat.stat_type_id = 4 self.save end diff --git a/app/serializers/api/v1/card_serializer.rb b/app/serializers/api/v1/card_serializer.rb index 6f3c9b2..284531e 100644 --- a/app/serializers/api/v1/card_serializer.rb +++ b/app/serializers/api/v1/card_serializer.rb @@ -1,7 +1,7 @@ class Api::V1::CardSerializer < Api::V1::BaseSerializer attributes :character, :rarity, :type, :leader_skill, :passive_skill, :super_attack, :awaken_type, :dokkan_card, :links, :title, - :gameid, :hp_stat, :atk_stat, :def_stat + :gameid, :hp_stat, :atk_stat, :def_stat, :cost_stat private @@ -63,6 +63,10 @@ class Api::V1::CardSerializer < Api::V1::BaseSerializer stat_formatter(object.def_stat) end + def cost_stat + stat_formatter(object.cost_stat) + end + private def stat_formatter(stat) diff --git a/app/views/admin/cards/_form.html.slim b/app/views/admin/cards/_form.html.slim index 7d36a90..3509f2a 100644 --- a/app/views/admin/cards/_form.html.slim +++ b/app/views/admin/cards/_form.html.slim @@ -26,6 +26,19 @@ .row .col-md-6 = f.association :dokkan_card, collection: Card.includes(:character).all, label_method: :full_name + .row + .col-md-12 + = f.label 'Cost' + .row + = f.simple_fields_for :cost_stat do |c| + .col-md-3 + = c.input :min + .col-md-3 + = c.input :max + .col-md-3 + = c.input :awaken_min + .col-md-3 + = c.input :awaken_max .row .col-md-12 = f.label 'Health Points' diff --git a/db/migrate/20151109165630_add_cost_stat_to_card.rb b/db/migrate/20151109165630_add_cost_stat_to_card.rb new file mode 100644 index 0000000..0aed230 --- /dev/null +++ b/db/migrate/20151109165630_add_cost_stat_to_card.rb @@ -0,0 +1,15 @@ +class AddCostStatToCard < ActiveRecord::Migration + def self.up + add_column :cards, :cost_stat_id, :integer + + PaperTrail.whodunnit = 1 + Card.all.each do |c| + c.cost_stat = Stat.new(stat_type_id: 4) + c.save + end + end + + def self.down + remove_column :cards, :cost_stat_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 557a322..5398b0e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20151103193404) do +ActiveRecord::Schema.define(version: 20151109165630) do create_table "awaken_types", force: :cascade do |t| t.string "name" @@ -37,6 +37,7 @@ ActiveRecord::Schema.define(version: 20151103193404) do t.integer "atk_stat_id" t.integer "def_stat_id" t.integer "links_count", default: 0, null: false + t.integer "cost_stat_id" end add_index "cards", ["awaken_type_id"], name: "index_cards_on_awaken_type_id" diff --git a/db/seeds.rb b/db/seeds.rb index 11dc24c..113168a 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -23,6 +23,7 @@ stat_types = [ ['HP', 'Health Points'], ['ATK', 'Attack'], ['DEF', 'Defense'], + ['Cost', 'Cost'], ] roles = [