diff --git a/app/controllers/statistics_controller.rb b/app/controllers/statistics_controller.rb
index 3d72bece..e05baadb 100644
--- a/app/controllers/statistics_controller.rb
+++ b/app/controllers/statistics_controller.rb
@@ -4,7 +4,7 @@ def sabeels
@apts = {}
- APARTMENTS.each do |apartment|
+ Sabeel::APARTMENTS.each do |apartment|
total_sabeels = Sabeel.send(apartment)
active_thaalis = total_sabeels.taking_thaali
inactive = total_sabeels.not_taking_thaali
@@ -12,9 +12,7 @@ def sabeels
@apts[apartment].store(:active_thaalis, active_thaalis.length)
@apts[apartment].store(:total_sabeels, total_sabeels.length)
@apts[apartment].store(:inactive_thaalis, inactive.length)
- SIZES.each do |size|
- @apts[apartment].store(size.to_sym, active_thaalis.with_thaali_size(size).length)
- end
+ Thaali::SIZES.each { @apts[apartment].store(_1, active_thaalis.with_thaali_size(_1).length) }
end
end
@@ -32,9 +30,7 @@ def thaalis
@years[y].store(:count, thaalis.count)
@years[y].store(:pending, Thaali.dues_unpaid_for(y).length)
@years[y].store(:complete, Thaali.dues_cleared_in(y).length)
- SIZES.each do |size|
- @years[y].store(size.to_sym, thaalis.send(size).count)
- end
+ Thaali::SIZES.each { @years[y].store(_1, thaalis.send(_1).count) }
end
end
end
diff --git a/app/controllers/thaalis_controller.rb b/app/controllers/thaalis_controller.rb
index 0fe84152..41e20d81 100644
--- a/app/controllers/thaalis_controller.rb
+++ b/app/controllers/thaalis_controller.rb
@@ -13,8 +13,7 @@ def new
if @sabeel.took_thaali?
took_thaali = @thaalis.where(year: PREV_YR).first
- @thaali.number = took_thaali[:number]
- @thaali.size = took_thaali[:size]
+ @thaali.attributes = took_thaali.slice(:number, :size)
end
end
@@ -24,7 +23,6 @@ def edit
def create
@sabeel = Sabeel.find(params[:sabeel_id])
@thaali = @sabeel.thaalis.new(create_params)
- @thaali.year = CURR_YR
if @thaali.save
Rails.cache.write("sabeel_#{@sabeel.id}_taking_thaali?", true)
diff --git a/app/models/concerns/array_extensions.rb b/app/models/concerns/array_extensions.rb
new file mode 100644
index 00000000..5091b163
--- /dev/null
+++ b/app/models/concerns/array_extensions.rb
@@ -0,0 +1,5 @@
+module ArrayExtensions
+ refine Array do
+ def to_h_titleize_value = to_h { [_1, _1.to_s.titleize] }
+ end
+end
diff --git a/app/models/role.rb b/app/models/role.rb
index 3a16d29c..3bff2656 100644
--- a/app/models/role.rb
+++ b/app/models/role.rb
@@ -1,18 +1,17 @@
class Role < ApplicationRecord
+ # * Constants
+ NAMES = %w[admin member viewer]
+
# rubocop:disable Rails/HasAndBelongsToMany
has_and_belongs_to_many :users, join_table: :users_roles
# rubocop:enable Rails/HasAndBelongsToMany
- belongs_to :resource,
- polymorphic: true,
- optional: true
+ belongs_to :resource, polymorphic: true, optional: true
- validates :resource_type,
- inclusion: {in: Rolify.resource_types},
- allow_nil: true
+ validates :resource_type, inclusion: {in: Rolify.resource_types}, allow_nil: true
validates :name, presence: true
- validates :name, inclusion: {in: ROLES}
+ validates :name, inclusion: {in: NAMES}
scopify
end
diff --git a/app/models/sabeel.rb b/app/models/sabeel.rb
index 8354bbc3..14f63b12 100644
--- a/app/models/sabeel.rb
+++ b/app/models/sabeel.rb
@@ -1,4 +1,10 @@
class Sabeel < ApplicationRecord
+ # * Constants
+ APARTMENTS = %i[mohammedi taiyebi burhani maimoon_a maimoon_b]
+
+ # * Defaults
+ attribute :apartment, default: nil
+
# * Associations
has_many :thaalis, dependent: :destroy
has_many :transactions, through: :thaalis
@@ -21,8 +27,10 @@ def sluggables = [its]
include ITSValidation
include NameValidation
+ using ArrayExtensions
+
# * Enums
- enum :apartment, APARTMENTS
+ enum apartment: APARTMENTS.to_h_titleize_value
# * Scopes
scope :no_thaali, -> { where.missing(:thaalis) }
diff --git a/app/models/thaali.rb b/app/models/thaali.rb
index 382512ec..85226369 100644
--- a/app/models/thaali.rb
+++ b/app/models/thaali.rb
@@ -1,6 +1,13 @@
class Thaali < ApplicationRecord
+ # * Constants
+ SIZES = %i[small medium large]
+
attr_readonly :total, :year
+ # * Defaults
+ attribute :year, default: CURR_YR
+ attribute :size, default: nil
+
# * Associtions
belongs_to :sabeel
has_many :transactions, dependent: :destroy
@@ -16,8 +23,10 @@ class Thaali < ApplicationRecord
def sluggables = [year, number]
+ using ArrayExtensions
+
# * Enums
- enum :size, SIZES
+ enum size: SIZES.to_h_titleize_value
# * Scopes
scope :dues_cleared_in, ->(year) {
diff --git a/app/models/transaction.rb b/app/models/transaction.rb
index 32ccdcd5..c20f5ef6 100644
--- a/app/models/transaction.rb
+++ b/app/models/transaction.rb
@@ -1,6 +1,12 @@
class Transaction < ApplicationRecord
+ # * Constants
+ MODES = %i[cash cheque bank]
+
default_scope { order(date: :desc) }
+ # * Defaults
+ attribute :mode, default: nil
+
# * Associations
belongs_to :thaali
@@ -16,8 +22,10 @@ class Transaction < ApplicationRecord
def sluggables = [recipe_no]
+ using ArrayExtensions
+
# * Enums
- enum :mode, MODES
+ enum mode: MODES.to_h_titleize_value
# * Scopes
scope :that_occured_on, ->(date) { where(date: date) }
diff --git a/app/models/user.rb b/app/models/user.rb
index 08d8937a..1d65f77e 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -25,8 +25,6 @@ def is?(type) = role == type
# * Custom Validations
def must_have_a_role
- unless roles.any?
- errors.add(:role_ids, "selection is required")
- end
+ errors.add(:role_ids, "selection is required") unless roles.any?
end
end
diff --git a/app/views/sabeels/_form.html.erb b/app/views/sabeels/_form.html.erb
index dc31b8e8..eadd65fd 100644
--- a/app/views/sabeels/_form.html.erb
+++ b/app/views/sabeels/_form.html.erb
@@ -3,7 +3,7 @@
<%= f.error_notification %>
<%= f.input :its %>
<%= f.input :name, placeholder: "Juzer Shabbir Shakir" %>
- <%= f.input :apartment, collection: APARTMENTS.map { [_1.to_s.titleize, _1] }, as: :radio_buttons %>
+ <%= f.input :apartment, collection: Sabeel.apartments.invert.to_a, as: :radio_buttons %>
<%= f.input :flat_no %>
<%= f.input :mobile, placeholder: "9876543210" %>
<%= f.input :email %>
diff --git a/app/views/thaalis/_form.html.erb b/app/views/thaalis/_form.html.erb
index 4d0c494a..7fab3c2c 100644
--- a/app/views/thaalis/_form.html.erb
+++ b/app/views/thaalis/_form.html.erb
@@ -1,11 +1,10 @@
- <%= simple_form_for @thaali, url: url_helper do |f| %>
+ <%= simple_form_for @thaali, url: path do |f| %>
<%= f.error_notification %>
- <%# for new thaali it will show current year %>
- <%= f.input :year, disabled: true, input_html: {value: (action_name == "edit") ? @thaali.year : CURR_YR} %>
+ <%= f.input :year, disabled: true %>
<%= f.input :number %>
- <%= f.input :size, collection: SIZES.map { [_1.capitalize, _1] }, as: :radio_buttons %>
+ <%= f.input :size, collection: Thaali.sizes.invert.to_a, as: :radio_buttons %>
<%= f.input :total, disabled: action_name == "edit" %>
<%= f.button :submit, action_name: %>
<% end %>
diff --git a/app/views/thaalis/edit.html.erb b/app/views/thaalis/edit.html.erb
index 9565b1e1..55b6c767 100644
--- a/app/views/thaalis/edit.html.erb
+++ b/app/views/thaalis/edit.html.erb
@@ -2,4 +2,4 @@
Edit Thaali
-<%= render "form", url_helper: thaali_path %>
+<%= render "form", path: thaali_path %>
diff --git a/app/views/thaalis/new.html.erb b/app/views/thaalis/new.html.erb
index 9d93268d..80878996 100644
--- a/app/views/thaalis/new.html.erb
+++ b/app/views/thaalis/new.html.erb
@@ -2,4 +2,4 @@
New Thaali
-<%= render "form", url_helper: sabeel_thaalis_path %>
+<%= render "form", path: sabeel_thaalis_path %>
diff --git a/app/views/transactions/_form.html.erb b/app/views/transactions/_form.html.erb
index 61f4e72a..2c9c8518 100644
--- a/app/views/transactions/_form.html.erb
+++ b/app/views/transactions/_form.html.erb
@@ -3,7 +3,7 @@
<%= 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 %>
+ <%= f.input :mode, collection: Transaction.modes.invert.to_a, as: :radio_buttons %>
<%= f.input :date, order: %i[day month year] %>
<%= f.button :submit, action_name: %>
<% end %>
diff --git a/config/initializers/constant.rb b/config/initializers/constant.rb
index 7c264c93..a8bb467e 100644
--- a/config/initializers/constant.rb
+++ b/config/initializers/constant.rb
@@ -1,7 +1,2 @@
-# ! Add new apartments, sizes & modes to the END of the list
-APARTMENTS = %i[mohammedi taiyebi burhani maimoon_a maimoon_b]
-SIZES = %i[small medium large]
-MODES = %i[cash cheque bank]
-ROLES = %w[admin member viewer]
CURR_YR = 2022
PREV_YR = CURR_YR - 1
diff --git a/db/migrate/20231230113604_replace_apartment_integer_with_apartment_enum_type_in_sabeel.rb b/db/migrate/20231230113604_replace_apartment_integer_with_apartment_enum_type_in_sabeel.rb
new file mode 100644
index 00000000..c6240f53
--- /dev/null
+++ b/db/migrate/20231230113604_replace_apartment_integer_with_apartment_enum_type_in_sabeel.rb
@@ -0,0 +1,29 @@
+class ReplaceApartmentIntegerWithApartmentEnumTypeInSabeel < ActiveRecord::Migration[7.1]
+ def change
+ apartments = ["Mohammedi", "Taiyebi", "Burhani", "Maimoon A", "Maimoon B"]
+ create_enum :apartments, apartments
+
+ add_column :sabeels, :apartment_enum, :enum, enum_type: :apartments, default: "Mohammedi", null: false
+
+ reversible do |direction|
+ # rubocop:disable Rails/SkipsModelValidations
+ direction.up do
+ apartments.each_with_index do |apartment, i|
+ Sabeel.where(apartment: i).update_all(apartment_enum: apartment)
+ end
+ end
+
+ direction.down do
+ apartments.each_with_index do |apartment, i|
+ Sabeel.where(apartment_enum: apartment).update_all(apartment: i)
+ end
+ # to access `#apartment` data, update the enum value of it in `app/models/sabeels` file:
+ # enum apartment: APARTMENTS
+ end
+ # rubocop:enable Rails/SkipsModelValidations
+ end
+
+ remove_column :sabeels, :apartment, :integer
+ rename_column :sabeels, :apartment_enum, :apartment
+ end
+end
diff --git a/db/migrate/20231230135852_replace_size_integer_with_size_enum_type_in_thaali.rb b/db/migrate/20231230135852_replace_size_integer_with_size_enum_type_in_thaali.rb
new file mode 100644
index 00000000..cb6364a2
--- /dev/null
+++ b/db/migrate/20231230135852_replace_size_integer_with_size_enum_type_in_thaali.rb
@@ -0,0 +1,29 @@
+class ReplaceSizeIntegerWithSizeEnumTypeInThaali < ActiveRecord::Migration[7.1]
+ def change
+ sizes = %w[Small Medium Large]
+ create_enum :sizes, sizes
+
+ add_column :thaalis, :size_enum, :enum, enum_type: :sizes, default: "Small", null: false
+
+ reversible do |direction|
+ # rubocop:disable Rails/SkipsModelValidations
+ direction.up do
+ sizes.each_with_index do |size, i|
+ Thaali.where(size: i).update_all(size_enum: size)
+ end
+ end
+
+ direction.down do
+ sizes.each_with_index do |size, i|
+ Thaali.where(size_enum: size).update_all(size: i)
+ end
+ # to access `#size` data, update the enum value of it in `app/models/thaalis` file:
+ # enum size: SIZES
+ end
+ # rubocop:enable Rails/SkipsModelValidations
+ end
+
+ remove_column :thaalis, :size, :integer
+ rename_column :thaalis, :size_enum, :size
+ end
+end
diff --git a/db/migrate/20231230171413_replace_mode_integer_with_mode_enum_type_in_transaction.rb b/db/migrate/20231230171413_replace_mode_integer_with_mode_enum_type_in_transaction.rb
new file mode 100644
index 00000000..b4e43fbb
--- /dev/null
+++ b/db/migrate/20231230171413_replace_mode_integer_with_mode_enum_type_in_transaction.rb
@@ -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
+ # to access `#mode` data, update the enum value of it in `app/models/transactions` file:
+ # enum mode: MODES
+ end
+ # rubocop:enable Rails/SkipsModelValidations
+ end
+
+ remove_column :transactions, :mode, :integer
+ rename_column :transactions, :mode_enum, :mode
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 924009da..fa6df687 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,10 +10,16 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema[7.0].define(version: 2023_11_02_042517) do
+ActiveRecord::Schema[7.1].define(version: 2023_12_30_171413) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
+ # Custom types defined in this database.
+ # Note that some types may not work with other database engines. Be careful if changing database.
+ create_enum "apartments", ["Mohammedi", "Taiyebi", "Burhani", "Maimoon A", "Maimoon B"]
+ create_enum "modes", ["Cash", "Cheque", "Bank"]
+ create_enum "sizes", ["Small", "Medium", "Large"]
+
create_table "friendly_id_slugs", force: :cascade do |t|
t.string "slug", null: false
t.integer "sluggable_id", null: false
@@ -38,13 +44,13 @@
create_table "sabeels", force: :cascade do |t|
t.integer "its", null: false
t.string "name", null: false
- t.integer "apartment", null: false
t.integer "flat_no", null: false
t.bigint "mobile", null: false
t.string "email"
t.string "slug", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
+ t.enum "apartment", default: "Mohammedi", null: false, enum_type: "apartments"
t.index ["its"], name: "index_sabeels_on_its", unique: true
t.index ["slug"], name: "index_sabeels_on_slug", unique: true
end
@@ -54,10 +60,10 @@
t.integer "year", null: false
t.integer "total", null: false
t.integer "number", null: false
- t.integer "size", null: false
t.string "slug", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
+ t.enum "size", default: "Small", null: false, enum_type: "sizes"
t.index ["sabeel_id"], name: "index_thaalis_on_sabeel_id"
t.index ["slug"], name: "index_thaalis_on_slug", unique: true
t.index ["year", "sabeel_id"], name: "index_thaalis_on_year_and_sabeel_id", unique: true
@@ -66,12 +72,12 @@
create_table "transactions", force: :cascade do |t|
t.bigint "thaali_id", null: false
t.integer "recipe_no", null: false
- t.integer "mode", null: false
t.integer "amount", null: false
t.date "date", null: false
t.string "slug", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
+ t.enum "mode", default: "Cash", null: false, enum_type: "modes"
t.index ["recipe_no"], name: "index_transactions_on_recipe_no", unique: true
t.index ["slug"], name: "index_transactions_on_slug", unique: true
t.index ["thaali_id"], name: "index_transactions_on_thaali_id"
diff --git a/db/seeds.rb b/db/seeds.rb
index 7f4bf252..df1756e8 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -5,10 +5,10 @@
Sabeel.create(
its: 10000000 + i,
name: Faker::Name.name,
- apartment: APARTMENTS.sample,
+ apartment: Sabeel::APARTMENTS.sample,
flat_no: Faker::Number.within(range: 1..9999),
mobile: Faker::Number.number(digits: 10),
- email: Faker::Internet.free_email
+ email: Faker::Internet.email
)
end
@@ -20,7 +20,7 @@
year: PREV_YR,
total: 48000,
number: i + 1,
- size: SIZES.sample
+ size: Thaali::SIZES.sample
)
end
@@ -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: Transaction::MODES.sample
)
end
end
@@ -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: Transaction::MODES.sample
)
end
end
@@ -61,7 +61,7 @@
year: CURR_YR,
total: 60000,
number: i + 1,
- size: SIZES.sample
+ size: Thaali::SIZES.sample
)
end
@@ -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: Transaction::MODES.sample
)
end
end
@@ -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: Transaction::MODES.sample
)
end
end
@@ -100,9 +100,7 @@
# *CREATES TOTAL Thaalis --> 175 (90 + 85)
# *CREATES TOTAL Transactions --> ~1.1k
-ROLES.each do |role|
- Role.create(name: role)
-end
+Role::NAMES.each { Role.create(name: _1) }
User.create(
its: 12345678,
diff --git a/spec/factories/roles.rb b/spec/factories/roles.rb
index 18174a4c..e04e648e 100644
--- a/spec/factories/roles.rb
+++ b/spec/factories/roles.rb
@@ -1,6 +1,6 @@
FactoryBot.define do
factory :role do
- name { ROLES.sample }
+ name { Role::NAMES.sample }
# * Factories
factory :admin do
diff --git a/spec/factories/sabeels.rb b/spec/factories/sabeels.rb
index 0da1a891..6756dbda 100644
--- a/spec/factories/sabeels.rb
+++ b/spec/factories/sabeels.rb
@@ -2,7 +2,7 @@
FactoryBot.define do
factory :sabeel do
- apartment { APARTMENTS.sample }
+ apartment { Sabeel::APARTMENTS.sample }
email { Faker::Internet.email }
flat_no { Faker::Number.within(range: 1..9999) }
# rubocop:disable RSpec/NoExpectationExample
diff --git a/spec/factories/thaalis.rb b/spec/factories/thaalis.rb
index cd588a19..1c441236 100644
--- a/spec/factories/thaalis.rb
+++ b/spec/factories/thaalis.rb
@@ -4,7 +4,7 @@
factory :thaali do
sabeel
number { Random.rand(1..10000) }
- sequence :size, SIZES.cycle
+ sequence :size, Thaali::SIZES.cycle
total { Faker::Number.number(digits: 5) }
year { Random.rand(1..CURR_YR) }
diff --git a/spec/factories/transactions.rb b/spec/factories/transactions.rb
index d7582438..618a6845 100644
--- a/spec/factories/transactions.rb
+++ b/spec/factories/transactions.rb
@@ -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, Transaction::MODES.cycle
factory :cleared_transaction, traits: [:cleared]
factory :today_transaction, traits: [:today]
diff --git a/spec/features/sabeels/new_spec.rb b/spec/features/sabeels/new_spec.rb
index f066aa2f..c255fc4b 100644
--- a/spec/features/sabeels/new_spec.rb
+++ b/spec/features/sabeels/new_spec.rb
@@ -21,7 +21,7 @@
it { expect(page).to have_title "New Sabeel" }
context "with valid values" do
- let(:apartment) { APARTMENTS.sample.to_s.titleize }
+ let(:apartment) { Sabeel.apartments.values.sample }
before do
choose apartment
diff --git a/spec/features/statistics/thaalis_spec.rb b/spec/features/statistics/thaalis_spec.rb
index f6112b3a..283bd381 100644
--- a/spec/features/statistics/thaalis_spec.rb
+++ b/spec/features/statistics/thaalis_spec.rb
@@ -47,7 +47,7 @@
it "each size" do
within("div##{CURR_YR}") do
- SIZES.each { expect(page).to have_content("#{_1.capitalize}: #{thaalis.send(_1).count}") }
+ Thaali::SIZES.each { |k, v| expect(page).to have_content("#{v}: #{thaalis.send(k).count}") }
end
end
diff --git a/spec/features/thaalis/new_spec.rb b/spec/features/thaalis/new_spec.rb
index fad27836..773742fe 100644
--- a/spec/features/thaalis/new_spec.rb
+++ b/spec/features/thaalis/new_spec.rb
@@ -47,7 +47,7 @@
context "with valid values" do
before do
- choose thaali[:size].capitalize
+ choose thaali[:size].to_s.titleize
click_button "Create Thaali"
end
diff --git a/spec/features/transactions/new_spec.rb b/spec/features/transactions/new_spec.rb
index 52aefde3..7f37fcda 100644
--- a/spec/features/transactions/new_spec.rb
+++ b/spec/features/transactions/new_spec.rb
@@ -22,9 +22,10 @@
context "with valid values" do
let(:new_transaction) { Transaction.last }
+ let(:mode) { Transaction.modes.values.sample }
before do
- choose MODES.sample.capitalize
+ choose mode
click_button "Create Transaction"
end
diff --git a/spec/features/users/new_spec.rb b/spec/features/users/new_spec.rb
index 5eafc564..da10969b 100644
--- a/spec/features/users/new_spec.rb
+++ b/spec/features/users/new_spec.rb
@@ -20,7 +20,7 @@
attributes_for(:user).each do |k, v|
case k
- when :roles then choose ROLES.sample.capitalize
+ when :roles then choose Role::NAMES.sample.titleize
else fill_in "user_#{k}", with: v
end
end
diff --git a/spec/models/role_spec.rb b/spec/models/role_spec.rb
index 0422e381..eba04d5d 100644
--- a/spec/models/role_spec.rb
+++ b/spec/models/role_spec.rb
@@ -9,7 +9,7 @@
context "with name" do
it { is_expected.to validate_presence_of(:name).with_message("selection is required") }
- it { is_expected.to validate_inclusion_of(:name).in_array(ROLES) }
+ it { is_expected.to validate_inclusion_of(:name).in_array(Role::NAMES) }
end
end
end
diff --git a/spec/models/sabeel_spec.rb b/spec/models/sabeel_spec.rb
index e8c5b9fa..b7257fd0 100644
--- a/spec/models/sabeel_spec.rb
+++ b/spec/models/sabeel_spec.rb
@@ -11,6 +11,12 @@
it { is_expected.to have_many(:transactions).through(:thaalis) }
end
+ context "with attributes" do
+ describe "set default values of" do
+ it { expect(described_class.new.apartment).to be_nil }
+ end
+ end
+
context "when validating" do
context "with email" do
it { is_expected.to validate_email_format_of(:email) }
@@ -18,11 +24,9 @@
end
context "with apartment" do
- let(:all_apartments) { described_class.apartments.keys }
-
it { is_expected.to validate_presence_of(:apartment).with_message("selection is required") }
- it { is_expected.to define_enum_for(:apartment).with_values(all_apartments) }
+ it { is_expected.to define_enum_for(:apartment).with_values(described_class::APARTMENTS.to_h { [_1, _1.to_s.titleize] }).backed_by_column_of_type(:enum) }
end
context "with flat_no" do
diff --git a/spec/models/thaali_spec.rb b/spec/models/thaali_spec.rb
index d6a5f07a..1bc65883 100644
--- a/spec/models/thaali_spec.rb
+++ b/spec/models/thaali_spec.rb
@@ -13,6 +13,11 @@
context "with attributes" do
it { is_expected.to have_readonly_attribute(:year) }
it { is_expected.to have_readonly_attribute(:total) }
+
+ describe "set default values of" do
+ it { expect(described_class.new.year).to eq(CURR_YR) }
+ it { expect(described_class.new.size).to be_nil }
+ end
end
context "when validating" do
@@ -22,7 +27,7 @@
end
context "with size" do
- it { is_expected.to define_enum_for(:size).with_values([:small, :medium, :large]) }
+ it { is_expected.to define_enum_for(:size).with_values(described_class::SIZES.to_h { [_1, _1.to_s.titleize] }).backed_by_column_of_type(:enum) }
it { is_expected.to validate_presence_of(:size).with_message("selection is required") }
end
diff --git a/spec/models/transaction_spec.rb b/spec/models/transaction_spec.rb
index 78b194e6..6fe779df 100644
--- a/spec/models/transaction_spec.rb
+++ b/spec/models/transaction_spec.rb
@@ -11,12 +11,17 @@
it { is_expected.to belong_to(:thaali) }
end
+ context "with attributes" do
+ describe "set default values of" do
+ it { expect(described_class.new.mode).to be_nil }
+ end
+ end
+
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(described_class::MODES.to_h { [_1, _1.to_s.titleize] }).backed_by_column_of_type(:enum) }
end
context "with amount" do
diff --git a/spec/requests/sabeels/active_spec.rb b/spec/requests/sabeels/active_spec.rb
index 96139f7c..af2666a2 100644
--- a/spec/requests/sabeels/active_spec.rb
+++ b/spec/requests/sabeels/active_spec.rb
@@ -3,7 +3,7 @@
require "rails_helper"
RSpec.describe "Thaali Active request" do
- let(:apartment) { APARTMENTS.sample }
+ let(:apartment) { Sabeel::APARTMENTS.sample }
# * NOT ACCESSIBLE
context "when made by logged out user" do
diff --git a/spec/requests/sabeels/inactive_spec.rb b/spec/requests/sabeels/inactive_spec.rb
index a2d526a2..bbac8579 100644
--- a/spec/requests/sabeels/inactive_spec.rb
+++ b/spec/requests/sabeels/inactive_spec.rb
@@ -3,7 +3,7 @@
require "rails_helper"
RSpec.describe "Thaali Inactive request" do
- let(:apartment) { APARTMENTS.sample }
+ let(:apartment) { Sabeel::APARTMENTS.sample }
# * NOT ACCESSIBLE
context "when made by logged out user" do