diff --git a/app/models/coupon.rb b/app/models/coupon.rb index 67db94b1d..0e3dfdd70 100644 --- a/app/models/coupon.rb +++ b/app/models/coupon.rb @@ -1,5 +1,5 @@ class Coupon < ActiveRecord::Base - has_many :invoice + has_many :invoices validates :code, presence: true validates :code, format: { with: /[A-Z0-9]+/ ,message: 'only caps letters and numbers'} diff --git a/app/views/api/coupons/_coupon.json.jbuilder b/app/views/api/coupons/_coupon.json.jbuilder index f66ec1ec6..b07ac132b 100644 --- a/app/views/api/coupons/_coupon.json.jbuilder +++ b/app/views/api/coupons/_coupon.json.jbuilder @@ -1 +1,2 @@ -json.extract! coupon, :id, :name, :code, :percent_off, :valid_until, :max_usages, :active, :created_at \ No newline at end of file +json.extract! coupon, :id, :name, :code, :percent_off, :valid_until, :max_usages, :active, :created_at +json.usages @coupon.invoices.count \ No newline at end of file diff --git a/app/views/api/coupons/index.json.jbuilder b/app/views/api/coupons/index.json.jbuilder new file mode 100644 index 000000000..b5f4a4142 --- /dev/null +++ b/app/views/api/coupons/index.json.jbuilder @@ -0,0 +1,5 @@ +json.cache! [@coupons] do + json.array!(@coupons) do |coupon| + json.partial! 'api/coupons/coupon', coupon: coupon + end +end diff --git a/db/migrate/20160804073558_remove_usages_from_coupons.rb b/db/migrate/20160804073558_remove_usages_from_coupons.rb new file mode 100644 index 000000000..4988a01b6 --- /dev/null +++ b/db/migrate/20160804073558_remove_usages_from_coupons.rb @@ -0,0 +1,5 @@ +class RemoveUsagesFromCoupons < ActiveRecord::Migration + def change + remove_column :coupons, :usages, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index f94ed49ef..31d8da9eb 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: 20160803104701) do +ActiveRecord::Schema.define(version: 20160804073558) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -110,7 +110,6 @@ ActiveRecord::Schema.define(version: 20160803104701) do t.integer "percent_off" t.datetime "valid_until" t.integer "max_usages" - t.integer "usages" t.boolean "active" t.string "stp_coupon_id" t.datetime "created_at", null: false