Change Newsletter's segment_recipient to string

Why:

Newsletter attribute `segment_recipient` is an integer to be used as
enum. There's no advantage to store a number instead of an string if the
ammount of elements in the table is not going to be huge, or we can take
advantage of using an enum.

Also maintaining both Newsletters enum paired with UserSegments::SEGMENTS
would be a maintenance burden.

How:

* Migration to change segment_recipient column from integer to string
* Removing enumeration from Newsletter model class
* Using UserSegments::SEGMENTS instead of Newsletter.segment_recipients
or integer values
This commit is contained in:
Bertocq
2018-02-20 22:38:49 +01:00
parent bdbb32e824
commit 4becd0eb35
10 changed files with 25 additions and 23 deletions

View File

@@ -8,7 +8,7 @@ section "Creating Newsletters" do
5.times do |n|
Newsletter.create!(
subject: "Newsletter subject #{n}",
segment_recipient: Newsletter.segment_recipients.values.sample,
segment_recipient: UserSegments::SEGMENTS.sample,
from: 'no-reply@consul.dev',
body: newsletter_body.sample,
sent_at: [Time.now, nil].sample

View File

@@ -0,0 +1,5 @@
class ChangeNewsletterSegmentRecipientToString < ActiveRecord::Migration
def change
change_column :newsletters, :segment_recipient, :string, null: false
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20180205170054) do
ActiveRecord::Schema.define(version: 20180220211105) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -626,7 +626,7 @@ ActiveRecord::Schema.define(version: 20180205170054) do
create_table "newsletters", force: :cascade do |t|
t.string "subject"
t.integer "segment_recipient"
t.string "segment_recipient", null: false
t.string "from"
t.text "body"
t.date "sent_at"