birdy
birdy

Reputation: 9646

How to find a method in rails code

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)

Upvotes: 0

Views: 110

Answers (2)

Emyl
Emyl

Reputation: 10536

In this case the Setting model is using method_missing, which is a special method called (if defined) when a NoMethodError exception is raised.

More info on method_missing here: http://rubylearning.com/satishtalim/ruby_method_missing.html

If you take a look to method_missing implementation in Setting: https://github.com/fatfreecrm/fat_free_crm/blob/master/app/models/setting.rb#L54-65

You'll find that Setting.user_signup will become Setting['user_signup'], so the [] method will be called with user_signup as 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.

Upvotes: 3

Erez Rabih
Erez Rabih

Reputation: 15788

Since Ruby is a dynamic language the best way to look for a method is to search for the string itself. Keep in mind that since methods may be created dynamically there may not be an actual def user_signup anywhere in the code, still the method might exist.

To search for the string across all project you can use your editor(whichever that is - all of them has this option), or just use grep -r user_signup . from your app root.

Upvotes: 0

Related Questions