Skip to content

Commit

Permalink
set default values for thaali
Browse files Browse the repository at this point in the history
  • Loading branch information
JuzerShakir committed Jan 2, 2024
1 parent b646eb2 commit c06efc4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
5 changes: 0 additions & 5 deletions app/controllers/thaalis_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ def show

def new
@thaali = @sabeel.thaalis.new
@thaali.year = CURR_YR

if @sabeel.took_thaali?
took_thaali = @thaalis.where(year: PREV_YR).first
@thaali.attributes = took_thaali.slice(:number, :size)
else
# uncheck the default option
@thaali.size = nil
end
end

Expand All @@ -27,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)
Expand Down
4 changes: 4 additions & 0 deletions app/models/thaali.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class Thaali < ApplicationRecord

attr_readonly :total, :year

# * Defaults
attribute :year, default: CURR_YR
attribute :size, default: nil

# * Associtions
belongs_to :sabeel
has_many :transactions, dependent: :destroy
Expand Down
5 changes: 5 additions & 0 deletions spec/models/thaali_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c06efc4

Please sign in to comment.