I am looking at some open source code to better familiarize myself with rails code.
I see the following in the application_controller
def can_signup?
[ :allowed, :needs_approval ].include? Setting.user_signup
end
I can’t seem to find where this method is defined in the code Setting.user_signup
Setting is a model
What can I be missing? link to code: https://github.com/fatfreecrm/fat_free_crm/blob/master/app/controllers/application_controller.rb#L155-157
Grep output:
/rails_projects/fat_free_crm $ grep -r --include="*.rb" "user_signup" .
./app/controllers/application_controller.rb: [ :allowed, :needs_approval ].include? Setting.user_signup
./app/controllers/users_controller.rb: if Setting.user_signup == :needs_approval
./app/models/users/user.rb: self.suspended? && self.login_count == 0 && Setting.user_signup == :needs_approval
./app/models/users/user.rb: self.suspended_at = Time.now if Setting.user_signup == :needs_approval && !self.admin
./spec/controllers/authentications_controller_spec.rb: Setting.stub!(:user_signup).and_return(:needs_approval)
./spec/controllers/users_controller_spec.rb: Setting.stub!(:user_signup).and_return(:needs_approval)
./spec/models/users/user_spec.rb: Setting.stub(:user_signup).and_return(:needs_approval)
./spec/models/users/user_spec.rb: Setting.stub(:user_signup).and_return(:needs_approval)
In this case the
Settingmodel is usingmethod_missing, which is a special method called (if defined) when aNoMethodErrorexception is raised.More info on
method_missinghere:http://rubylearning.com/satishtalim/ruby_method_missing.html
If you take a look to
method_missingimplementation inSetting:https://github.com/fatfreecrm/fat_free_crm/blob/master/app/models/setting.rb#L54-65
You’ll find that
Setting.user_signupwill becomeSetting['user_signup'], so the[]method will be called withuser_signupas first argument:https://github.com/fatfreecrm/fat_free_crm/blob/master/app/models/setting.rb#L69-84
That method (
[]), according to documentation, will search a setting within a database table or a .yml file with the name equal to the first argument, and then returns the relative value.