A model method starts with the following logic:
def calculate_vat
if self.country.blank?
flash[:danger] = "Please select country first"
redirect_to edit_organization_path(self) and return
end
if ["BE", "CZ", "ES", "LV", "LT"].include? self.country
return 0.21
elsif etc. for other countries.
end
end
Is this not possible within a model method? In the controller, the model method gets called upon using @organization.model_method
. It produces the following error in development: undefined local variable or method 'flash' for #<Organization:0x00000007298a68>
.
Update: I understand now that it's impossible to use flash and redirect in model method. But how to solve this? Only a specific group of users will ever deal with this model method; so I don't want to make country
required for all users. A custom requirement seems impossible because there are no model variables upon which to base such a validation, it's a matter whether a user will ever get to certain checkout pages.
Would the best solution perhaps be to define a private controller method that is called upon from inside the controller just before calculate_vat
is called upon?