0

I am using meta_search as follows:

# app/controllers/articles_controller.rb
def index
  @search = Article.search(params[:search])
  @articles = @search.all
end

# app/views/articles/index.html.erb
<%= form_for @search, :url => articles_path, :html => {:method => :get} do |f| %>
  <%= f.text_field :my_very_long_attribute_name_contains %><br />
  <%= f.submit %>
<% end %>

This works as expected, by allowing the 'my_very_long_attribute_name' attribute to be searched.

The problem is, ?search[my_very_long_attribute_name_contains] appears in the query string. What is the best way to map a shorter name to this attribute? i.e. ?search[mvlan_contains]

This isn't just a case of wanting to make long attribute names shorter, but I also need to disguise the names of some potentially sensitive attributes for search purposes.

I have looked at alias_attribute, but couldn't get meta_search to recognise the attribute alias.

I welcome any suggestions.

gjb
  • 6,237
  • 7
  • 43
  • 71
  • 1
    Haven't used it myself, but take a look at the "Accessing custom search methods" section of the README - https://github.com/ernie/meta_search – Robert Speicher Dec 08 '10 at 18:37
  • Thanks. While this is useful, it doesn't enable me to create aliases for my attributes. – gjb Dec 08 '10 at 23:57

1 Answers1

1

You could do as rspeicher suggested. The custom search method will be displayed in your parameter list.

Re: sensitive info in your attributes, though... If someone knowing your attribute names in the view poses a potential problem for your application, I'd seriously consider what security steps are being taken in your model and controller layers.

Ernie
  • 896
  • 5
  • 7
  • I have added added `def mvlan; my_very_long_attribute_name; end` and `search_methods :mvlan` to my model, but I am getting 'undefined method' when I try to use `<%= f.text_field :mvlan_eq %>` in my view. What am I doing wrong? – gjb Dec 09 '10 at 18:23
  • You're adding eq. When you define a custom method you're saying you want to take control over how the parameter is used in your code. There's no _eq added. You are supposed to handle the relation modification yourself, and then return that relation for chaining. – Ernie Dec 09 '10 at 19:10
  • I can't use `search_methods` to do this then. Is there another way of achieving this? Out of interest, if I do `alias_attribute :mvlan, :my_very_long_attribute_name`, why can't I then use `<%= f.text_field :mvlan_eq %>` in my view? – gjb Dec 10 '10 at 00:34
  • MetaSearch attributes tie back to columns since they need the type info. I've developed quite a few rails apps in my time and never once bothered with alias_attribute, so I'm not sure whether it retains that info or not. I can check into this but again strongly suggest you examine controller/model level security if attribute name knowledge poses real risk to your application. – Ernie Dec 10 '10 at 01:25
  • I would be extremely grateful if it is something that can be added easily. You've done an awesome job in creating MetaSearch -- this is the only point I've run into difficulty with. It's not that the attribute names are sensitive per se, but they mirror those of a third party data provider that I am using. If at all possible, I'd like to hide these names so that users can't make the connection. – gjb Dec 10 '10 at 12:04
  • Just wanted to follow up here -- I looked into it and alias_attribute is just a macro to create a method that calls your attribute method. There's not any real "aliasing" going on at the column level, so I won't be able to make a simple change to support this. – Ernie Dec 17 '10 at 21:16