#YepSearchable
This is a ruby gem to search in Ruby models as ActiveRecord::Base and ActiveResources::Base.
Beta notes: The search is temporarily working as “contains” clauses.
##Installing
In your Gemfile, add the following line of code: <pre>gem ‘yep_searchable’</pre>
##Configuring
### ActiveRecord Search In your ActiveRecord model, add this line of code: <pre>searchable_resource searchable_columns: [:name, :notes]</pre>
For example: “‘ruby class Brand < ActiveRecord::Base
searchable_resource searchable_columns: [:name]
....
end “‘
“‘ruby class Car < ActiveRecord::Base
searchable_resource searchable_columns: [:model, :color]
belongs_to :brand
....
end “‘
### ActiveResource Search Your ActiveResource must implement YepSearchable::YepResource, like this:
“‘ruby class Brand < YepSearchable::YepResource
self.site = "http://127.0.0.1"
...
end “‘
In YepResource you have two new attributes: “‘ruby
...
self.search_path = "search_resource" # this is the default value
self.search_ids_path = "search_ids" # this is the default value
...
“‘
For each of this path you must create an route and an action on source controller. Example:
“‘ruby class BrandController < ApplicationController
...
# GET /brands/search_resource
def search_resource
@brands = Brand.search(params[:query])
render json: @brands
end
# GET /brands/search_ids
def search_ids
ids = Brand.search_only_ids(params[:query])
render json: ids
end
...
end “‘
### Declaring relations between ActiveRecord and YepResource Once you search for “Ferrari” you are expect to receive the cars branded by Ferrari. So in your ActiveRecord “searchable_resource” declaration, you should add the following option: “‘ruby searchable_resource searchable_columns: [:name, :notes], yep_resources: [:brand, foreign_key: :brand_id] “`
In this case, if you search like this: “‘ruby Car.search(“ferrari”) “`
The result will be all cars that contain “Ferrari” in the model, color or the brand_id equal for the brands that contains “ferrari” in the name. Comparing to an SQL command, is something like: “‘sql SELECT * FROM cars WHERE cars.model LIKE ’%FERRARI%‘ OR cars.color LIKE ’%FERRARI%‘ –here is the trick OR (cars.brand_id IN (SELECT id FROM brands WHERE brands.name LIKE ’%FERRARI%‘)); “`
##Usage The yep_searchable gem provide differents way for search. You can give more than one parameter or not propagate the search. The examples will describe the gem methods.
### Searching
-
You can search with more than one parameter:
“‘ruby Car.search(“ferrari”, “red”) # this will look for all cars that contain “ferrari” or “red” in the name, color or brand name “`
-
You are able to not propagate the search, the search will not look for the relations:
“‘ruby Car.search(false, “red”) # this will look for all cars that contain “red” in the name or color BUT WILL NOT SEARCH FOR BRANDS THAT CONTAINS “RED” IN THE NAME “`
### Searching only IDS
-
You can search only the model ids using the method “search_only_ids”:
“‘ruby Car.search_only_ids(“ferrari”, “red”) # this will look for all cars ids that contain “ferrari” or “red” in the name, color or brand name “`
-
As the normal search, you can propagate the search or not:
“‘ruby Car.search_only_ids(false, “red”) # this will look for all cars ids that contain “red” in the name or color BUT WILL NOT SEARCH FOR BRANDS THAT CONTAINS “RED” IN THE NAME “`
### Pagination
-
You can paginate your search using the method “search_by_page”, the search by page method have the following parameters:
-
only_ids - default false
-
page_number - default nil
-
number_of_rows - default nil - number of rows per page
-
propagate - default true
Example:
“‘ruby Car.search_by_page(false, 1, 30, true, “ferrari”, “red”) # this will look for the first page, 30 cars # that contain “ferrari” or “red” in the name, color or brand name “`
### Is searchable resource?
-
The YepSearchable gem provides the capability to check if a Model is searchable by the method “is_searchable_resource?”
Example:
“‘ruby Car.is_searchable_resource? # => true Motorcycle.is_searchable_resource? # => false “`