Swags stored correctly

This commit is contained in:
nasia 2017-08-26 20:41:36 +03:00
parent 2042d0eb7a
commit 4381f687b6
7 changed files with 18 additions and 28 deletions

View file

@ -11,12 +11,12 @@ module Admin
end
def show
@sponsor.swag_index = @sponsor.swag_hash.length
@sponsor.swag_index = @sponsor.swags.length
end
def edit
@sponsor.swag_index = @sponsor.swag_hash.length
@sponsor.swag_hash = @sponsor.swag_hash
@sponsor.swag_index = @sponsor.swags.length
@sponsor.swags = @sponsor.swags
end
def new
@ -63,7 +63,7 @@ module Admin
def get_swag_hash; end
def get_swags; end
def paid
@sponsor.paid = !@sponsor.paid
@ -96,7 +96,7 @@ module Admin
def sponsor_params
params.require(:sponsor).permit(:name, :description, :website_url, :picture, :picture_cache, :sponsorship_level_id, :conference_id,
:paid, :has_swag, :swag_received, :address, :vat, :has_banner, :swag_hash, :type, :quantity, :swag_index)
:paid, :has_swag, :swag_received, :address, :vat, :has_banner, :swag_index, swags: [:type, :quantity])
end
def sponsorship_level_required

View file

@ -2,7 +2,7 @@ class Sponsor < ActiveRecord::Base
belongs_to :sponsorship_level
belongs_to :conference
serialize :swag_hash, Hash
serialize :swags, Hash
attr_accessor :type, :quantity, :swag_index, :hint_hash
has_paper_trail ignore: [:updated_at], meta: { conference_id: :conference_id }
@ -10,13 +10,4 @@ class Sponsor < ActiveRecord::Base
mount_uploader :picture, PictureUploader, mount_on: :logo_file_name
validates :name, :website_url, :sponsorship_level, presence: true
def generate_swags_hash(type, quantity)
unless swag_hash
swag_hash = {}
end
swag_hash[type.to_s] = quantity.to_i
update_attribute(:swag_hash, @sponsor.swag_hash)
end
end

View file

@ -22,10 +22,9 @@
off_color: 'warning',
on_text: 'Yes',
off_text: 'No' }
- @sponsor.swag_hash.each_with_index do |hash, index|
=render partial: 'swag_fields', locals: {f: f, index: index}
=render partial: 'swag_fields', locals: { f: f, index: @sponsor.swag_index}
- @sponsor.swags.each_with_index do |(key, value), index|
=render partial: 'swag_fields', locals: {f: f, index: index, v_type: value[:type].to_s, v_quantity: value[:quantity].to_i}
=render partial: 'swag_fields', locals: { f: f, index: @sponsor.swag_index, v_type: nil, v_quantity: 0}
.row
.col-md-8

View file

@ -1,5 +1,5 @@
.row
.col-md-6
= f.input :type, label: 'Swag\'s type', input_html: { name: "sponsor[swag_hash][#{index}][type]"}
= f.input :type, label: 'Swag\'s type', input_html: { name: "sponsor[swags][#{index}][type]", value: v_type }
.col-md-6
= f.input :quantity, input_html: { name: "sponsor[swag_hash][#{index}][quantity]"}
= f.input :quantity, as: :number, in: 0...999, input_html: { name: "sponsor[swags][#{index}][quantity]", value: v_quantity}

View file

@ -69,12 +69,12 @@
%b Swag's Type
%td
%b Quantity
%tr
- @sponsor.swag_hash.each do |key, value|
- @sponsor.swags.each do |key, value|
%tr
%td.col-md-2
= key
= value[:type]
%td
= value
= value[:quantity]

View file

@ -6,6 +6,6 @@ class AddSwagsToSponsors < ActiveRecord::Migration
add_column :sponsors, :address, :string
add_column :sponsors, :vat, :string
add_column :sponsors, :has_banner, :boolean, default: false
add_column :sponsors, :swag_hash, :text
add_column :sponsors, :swags, :text
end
end

View file

@ -469,17 +469,17 @@ ActiveRecord::Schema.define(version: 20170822173332) do
t.datetime "updated_at"
t.string "picture"
t.boolean "payed", default: false
t.boolean "swags", default: false
t.boolean "swags_received"
t.string "company_address"
t.string "vat_registration"
t.text "swag_hash"
t.boolean "paid", default: false
t.boolean "has_swag", default: false
t.boolean "swag_received"
t.string "address"
t.string "vat"
t.boolean "has_banner", default: false
t.text "swag_hash"
t.text "swags"
end
create_table "sponsorship_levels", force: :cascade do |t|