Skip to content

Commit

Permalink
switch transaction#mode from integer type to enum type
Browse files Browse the repository at this point in the history
  • Loading branch information
JuzerShakir committed Dec 30, 2023
1 parent daf6152 commit 86040c1
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/models/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Transaction < ApplicationRecord
def sluggables = [recipe_no]

# * Enums
enum :mode, MODES
enum mode: MODES

# * Scopes
scope :that_occured_on, ->(date) { where(date: date) }
Expand Down
6 changes: 5 additions & 1 deletion app/views/transactions/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
<%= f.error_notification %>
<%= f.input :recipe_no %>
<%= f.input :amount, hint: "Amount shouldn't be greater than: ₹#{total_balance}" %>
<%= f.input :mode, collection: MODES.map { [_1.capitalize, _1] }, as: :radio_buttons %>
<% if action_name == "edit" %>
<%= f.input :mode, collection: MODES.keys.map { [_1.capitalize, _1] }, as: :radio_buttons %>
<% else %>
<%= f.input :mode, input_html: {checked: false}, collection: MODES.keys.map { [_1.capitalize, _1] }, as: :radio_buttons %>
<% end %>
<%= f.input :date, order: %i[day month year] %>
<%= f.button :submit, action_name: %>
<% end %>
Expand Down
7 changes: 6 additions & 1 deletion config/initializers/constant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
large: "Large"
}

MODES = %i[cash cheque bank]
MODES = {
cash: "Cash",
cheque: "Cheque",
bank: "Bank"
}

ROLES = %w[admin member viewer]
CURR_YR = 2022
PREV_YR = CURR_YR - 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class ReplaceModeIntegerWithModeEnumTypeInTransaction < ActiveRecord::Migration[7.1]
def change
modes = %w[Cash Cheque Bank]
create_enum :modes, modes

add_column :transactions, :mode_enum, :enum, enum_type: :modes, default: "Cash", null: false

reversible do |direction|
# rubocop:disable Rails/SkipsModelValidations
direction.up do
modes.each_with_index do |mode, i|
Transaction.where(mode: i).update_all(mode_enum: mode)
end
end

direction.down do
modes.each_with_index do |mode, i|
Transaction.where(mode_enum: mode).update_all(mode: i)
end
# make sure to update the MODES constant value in order to access the 'mode' data
# MODES = %i[cash cheque bank]
end
# rubocop:enable Rails/SkipsModelValidations
end

remove_column :transactions, :mode, :integer
rename_column :transactions, :mode_enum, :mode
end
end
5 changes: 3 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
amount: 4000,
date: Faker::Date.in_date_period(year: PREV_YR),
recipe_no: Random.rand(1..2000000),
mode: MODES.sample
mode: MODES.keys.sample
)
end
end
Expand All @@ -48,7 +48,7 @@
amount: 4000,
date: Faker::Date.in_date_period(year: PREV_YR),
recipe_no: Random.rand(2000001..3000000),
mode: MODES.sample
mode: MODES.keys.sample
)
end
end
Expand All @@ -75,7 +75,7 @@
amount: 5000,
date: Faker::Date.in_date_period(year: CURR_YR),
recipe_no: Random.rand(3000001..6000000),
mode: MODES.sample
mode: MODES.keys.sample
)
end
end
Expand All @@ -91,7 +91,7 @@
amount: 5000,
date: Faker::Date.in_date_period(year: CURR_YR),
recipe_no: Random.rand(6000000..10000000),
mode: MODES.sample
mode: MODES.keys.sample
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
amount { Faker::Number.number(digits: 4) }
date { Faker::Date.backward }
recipe_no { Random.rand(1000..100000) }
sequence :mode, MODES.cycle
sequence :mode, MODES.keys.cycle

factory :cleared_transaction, traits: [:cleared]
factory :today_transaction, traits: [:today]
Expand Down
2 changes: 1 addition & 1 deletion spec/features/transactions/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
let(:new_transaction) { Transaction.last }

before do
choose MODES.sample.capitalize
choose MODES.values.sample
click_button "Create Transaction"
end

Expand Down
5 changes: 2 additions & 3 deletions spec/models/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@

context "when validating" do
context "with mode" do
let(:mode_of_payments) { %i[cash cheque bank] }

it { is_expected.to validate_presence_of(:mode).with_message("selection is required") }
it { is_expected.to define_enum_for(:mode).with_values(mode_of_payments) }

it { is_expected.to define_enum_for(:mode).with_values(MODES).backed_by_column_of_type(:enum) }
end

context "with amount" do
Expand Down

0 comments on commit 86040c1

Please sign in to comment.