0

I am new to rails development. I have created some aliases to a method and I want to know that which alias is called.

I have this code.

alias_method :net_stock_quantity_equals :net_stock_quantity
alias_method :net_stock_quantity_gte :net_stock_quantity
alias_method :net_stock_quantity_lte :net_stock_quantity
alias_method :net_stock_quantity_gt :net_stock_quantity
alias_method :net_stock_quantity_lt :net_stock_quantity

def net_stock_quantity
  #some code here
end

I want to know that user has called which alias. Like if user calls net_stock_quantity_equals then I should know that the user has called net_stock_quantity_equals not net_stock_quantity.

Any help would be appreciated.

Baylor Rae'
  • 3,995
  • 20
  • 39
Kashif Umair Liaqat
  • 107
  • 1
  • 1
  • 11
  • 2
    You're the one aliasing the method--sounds more like you actually want to generate a *real* method and do something based on which method is called. All alias_method does is alias a method. You're trying to do something different. – Dave Newton Sep 05 '12 at 15:20
  • If you're trying to filter records dynamically you might find the "Ransack" gem helpful. https://github.com/ernie/ransack – Baylor Rae' Sep 05 '12 at 15:23
  • Yes I'm trying to minimize the methods in my model. I want to use if conditions in `net_stock_quantity` on the bases of which alias is called. Is it a better approach? If yes then tell me how can I do this and if it is not then tell me the alternative and better approach. Thanks. – Kashif Umair Liaqat Sep 05 '12 at 15:25
  • @BaylorRae' I know about Ransack but I'm already using ernie/meta_search gem. – Kashif Umair Liaqat Sep 05 '12 at 15:27
  • @KashifUmairLiaqat then can you use the `search` method in your controller (or where every you're using these methods)? – Baylor Rae' Sep 05 '12 at 15:31
  • @BaylorRae', if you have read about custom search methods in meta_search then you can understand better. This is a custom search method. How can I use a single custom search method for all the conditions i.e. equals, greate_than_equals, less_than_equals, less_than, greater_than? – Kashif Umair Liaqat Sep 05 '12 at 15:34
  • @KashifUmairLiaqat I don't think you are supposed to add the methods to your model. Those methods are used by meta search to find records. – Baylor Rae' Sep 05 '12 at 15:47
  • @BaylorRae' Then where should I add these methods? – Kashif Umair Liaqat Sep 06 '12 at 06:19
  • @KashifUmairLiaqat you don't need to add the methods because they won't be called on your model. – Baylor Rae' Sep 06 '12 at 12:15
  • @BaylorRae' please have a look at [http://github.com/ernie/meta_search](http://github.com/ernie/meta_search). Here it is clearly mentioned that if you want to add custom search methods you can add the named scopes to your model. I added 5 named scopes, one for each condition, and they worked perfectly. But now I want to minimize my code by just using one method for all those conditions. – Kashif Umair Liaqat Sep 06 '12 at 13:07
  • @KashifUmairLiaqat Are you trying to search something from an association? Could you please provide more code from your model so I can have a better understanding of what you're trying to accomplish. – Baylor Rae' Sep 06 '12 at 14:41

3 Answers3

2

You could override method_missing to do it.

def method_missing(method_name, *args, &block)
  if method_name.to_s =~ /^net_stock_quantity_/ 
    net_stock_quantity method_name
  else
    super
  end
end

def net_stock_quantity(alias_used = :net_stock_quantity)
  #some code
end

There's a tutorial here doing something similar http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-missing-methods/

kits
  • 476
  • 2
  • 4
  • Very nice answer and I am sure it works too. But there is a simple solution too. I will write that solution in a new answer to this question because the code cannot be properly displayed in comments. – Kashif Umair Liaqat Sep 07 '12 at 09:33
1

It think you are approaching the problem incorrectly - instead of using alias methods, send through :equals, :gte, :lte etc. as a parameter to the method ie:

def net_stock_quantity(type = :all)
  # do something with the type here
end 
sachinr
  • 189
  • 3
  • Sachnir, if you go through the meta_search documentation you will be clear that we can't send those parameters(:equals, :gte, :lte) to a custom search method and if we can then tell me how? – Kashif Umair Liaqat Sep 06 '12 at 06:21
0
def net_stock_quantity(alias_used = :net_stock_quantity)
    method_called = caller[0]
    #some code
end

The method_called will contain the name of called alias.

Kashif Umair Liaqat
  • 107
  • 1
  • 1
  • 11