Skip to content

Commit

Permalink
BUG FIX - year & total fields are now read-only
Browse files Browse the repository at this point in the history
  • Loading branch information
JuzerShakir committed Dec 26, 2023
1 parent e08eb0d commit cb8ec9c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 5 deletions.
8 changes: 5 additions & 3 deletions app/controllers/thaalis_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def edit

def create
@sabeel = Sabeel.find(params[:sabeel_id])
@thaali = @sabeel.thaalis.new(thaali_params)
@thaali = @sabeel.thaalis.new(create_params)
@thaali.year = CURR_YR

if @thaali.save
Expand All @@ -41,7 +41,7 @@ def create
end

def update
if @thaali.update(thaali_params)
if @thaali.update(update_params)
redirect_to @thaali, success: t(".success")
else
render :edit, status: :unprocessable_entity
Expand Down Expand Up @@ -103,7 +103,7 @@ def check_thaali_for_current_year

def set_year = @year = params[:year]

def thaali_params = params.require(:thaali).permit(:number, :size, :total)
def create_params = params.require(:thaali).permit(:number, :size, :total)

def turbo_load(thaalis)
respond_to do |format|
Expand All @@ -113,4 +113,6 @@ def turbo_load(thaalis)
end
end
end

def update_params = params.require(:thaali).permit(:number, :size)
end
2 changes: 2 additions & 0 deletions app/models/thaali.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Thaali < ApplicationRecord
attr_readonly :total, :year

# * Associtions
belongs_to :sabeel
has_many :transactions, dependent: :destroy
Expand Down
2 changes: 1 addition & 1 deletion app/views/thaalis/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<%= f.input :year, disabled: true, input_html: {value: (action_name == "edit") ? @thaali.year : CURR_YR} %>
<%= f.input :number %>
<%= f.input :size, collection: SIZES.map { [_1.capitalize, _1] }, as: :radio_buttons %>
<%= f.input :total %>
<%= f.input :total, disabled: action_name == "edit" %>
<%= f.button :submit, action_name: %>
<% end %>
</div>
2 changes: 1 addition & 1 deletion config/initializers/new_framework_defaults_7_1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
# behavior would allow assignment but silently not persist changes to the
# database.
#++
# Rails.application.config.active_record.raise_on_assign_to_attr_readonly = true
Rails.application.config.active_record.raise_on_assign_to_attr_readonly = true

###
# Enable validating only parent-related columns for presence when the parent is mandatory.
Expand Down
5 changes: 5 additions & 0 deletions config/locales/simple_form.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ en:
mobile: "without the country code"
year: "You cannot change this field"
name: "between 3 to 35 characters long"
thaali:
new:
total: "Cannot be edited later"
edit:
total: "You cannot change this field"
user:
password: "between 6 to 72 characters long"
password_confirmation: "should match the password"
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 @@ -10,6 +10,11 @@
it { is_expected.to have_many(:transactions).dependent(:destroy) }
end

context "with attributes" do
it { is_expected.to have_readonly_attribute(:year) }
it { is_expected.to have_readonly_attribute(:total) }
end

context "when validating" do
context "with number" do
it { is_expected.to validate_numericality_of(:number).only_integer.with_message("must be an integer") }
Expand Down

0 comments on commit cb8ec9c

Please sign in to comment.