Passing a variable into a models after_initialize method - ruby-on-rails

I have the following (heavily simplified) model, that uses will_paginate
class Search < ActiveRecord::Base
attr_reader :articles
def after_initialize
#articles = Article.paginate_by_name name, :page => 1
end
end
and the controller code in my show action is
#search = Search.new(params[:search])
This all works fine, but notice i hard coded the page number to 1, problem is passing params[:page] value into the after_initialize method, can anyone suggest an elegant way to do this please?
Thanks

Add a page parameter (or even better an options hash parameter) to the initialize method:
class Search
def initialize(search, options = {})
#options = options
end
def after_initialize
#articles = Article.paginate_by_name name, :page => #options[:page]
end
end
and then in your controller:
#search = Search.new(params[:search], :page => params[:page])
You can even supply default values to the option hash, if you like.

Related

ActiveAdmin limit records on Index page

I have some 10,000+ records in my model. In active_admin index page for that model I have set config.paginate = false. So all the 10,000+ records are shown by default.
How can I limit the number to say last 500 records. I have tried using the below method described here, but its not doing anything to the index page.
ActiveAdmin.register Post do
controller do
def scoped_collection
Post.all.limit(500)
end
end
end
set custom # of rows on page with controller before_filter
controller do
before_filter :set_per_page_var, :only => [:index]
def set_per_page_var
session[:per_page]=params[:per_page]||30
#per_page = session[:per_page]
end
end
and render sidebar with corresponding text input (you can render it as a drop-list)
#...
sidebar('Rows on page', :only => :index) do
form do |f|
f.text_field nil, 'per_page', :value => session[:per_page]
end
end
The issue is this code in Active Admin:
module ActiveAdmin
class ResourceController < BaseController
module DataAccess
def per_page
return max_csv_records if request.format == 'text/csv'
return max_per_page if active_admin_config.paginate == false
#per_page || active_admin_config.per_page
end
def max_csv_records
10_000
end
def max_per_page
10_000
end
end
end
end
When the paginate config option is set to false, it defaults to the number value returned by max_per_page. If you're fine with overriding it globally, you can put this in an initializer:
# config/initializers/active_admin_data_access.rb
module ActiveAdmin
class ResourceController < BaseController
module DataAccess
def max_per_page
500 # was 10,000
end
end
end
end
I was looking for an answer to this same question. I was unable to limit the number of records, so instead I have opted for putting a default value in one of my filters that guarantees an empty page when it loads.
(NOTE: I stole this idea from this stackoverflow question here:: Set ActiveAdmin filter default value )
Example::
In this example, I set a filter called "my_filter_id" equal to "0" in the "before_filter" method if all of the parameters are blank.
ActiveAdmin.register MyModel do
before_filter my_filter_id: :index do
params[:q] = {my_filter_id_eq: 0} if params[:commit].blank?
end
end
Use
Post.limit(500) instead of Post.all.limit(500) so it will minimize the latency.
controller do
def scoped_collection
Post.limit(500)
end
end
index :pagination_total => false do
selectable_column
column :id
column :user_name
column :country
column :city
end
Hope this will help someone.
Try below code. Replace something with your model name.
result = Something.find(:all, :order => "id desc", :limit => 5)
while !result.empty?
puts result.pop
end

Correct way to validate the query string parameter in controller?

I have a User controller and method named 'index' for listing users. And in view there is a filtering option (with status pending, active, deleted). I wrote the code and its working fine. But I need to know the code that I wrote is correct or not, or is any easy method to code. Is that correct method for validating status in controller(validating if the query string status include in a set of statuses). Please help
I used following code:
class UsersController < ApplicationController
def index
#filter_field = ''
if ((not params[:status].nil?) && ['pending', 'active', 'deleted'].include?(params[:status]))
#filter_field = params[:status]
end
#users = User.select_all(#filter_field)
end
end
class User < ActiveRecord::Base
def self.select_all filter
if filter.empty?
User.find(:all)
else
User.find(:all, :conditions => ['status = ?', filter]))
end
end
end
You can do this:
def index
filter = %w(pending active deleted).include?(params[:status]) ? params[:status] : ''
#users = User.select_all(filter)
end
It makes the check easier to understand + no need for an instance variable right?
BTW, I feel like:
['pending', 'active', 'deleted']
Should be given by a method, to avoid a magic array

Where do I put a rails method/scope that uses params?

I have a scope that uses RubyGeocoder method, near, to filter events by location using param[:searchCity]. The param gets the user's geolocation so it shows events only near them. I currently have it working in my events_controller index action, but I also need to call it on my home page.
Considering it's a filter that gets data from the database, I thought it would be best to go in the model, but I'm finding conflicting information on whether having a param in the model is ok or bad practice. Also, I can't get it to work in the model with the param present.
What's the best practice for something like this? Where should I place the scope, the model, controller, helper, or somewhere else?
Here's my code:
Model:
class Event < ActiveRecord::Base
# attr, validates, belongs_to etc here.
scope :is_near, self.near(params[:searchCity], 20, :units => :km, :order => :distance) #doesn't work with the param, works with a "string"
end
Controller:
def index
unless params[:searchCity].present?
params[:searchCity] = request.location.city
end
#events = Event.is_near
# below works in the controller, but I don't know how to call it on the home page
# #events = Event.near(params[:searchCity], 20, :units => :km, :order => :distance)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #events }
end
end
The line I'm calling in my home page that gets how many events are in the area
<%= events.is_near.size %>
Edit: Using a lambda seems to be working. Is there any reason I shouldn't do it this way?
Model:
class Event < ActiveRecord::Base
scope :is_near, lambda {|city| self.near(city, 20, :units => :km, :order => :distance)}
end
Controller:
def index
#events = Event.is_near(params[:searchCity])
...
home.html.erb
<%= events.is_near(params[:searchCity]).size %>
Accessing the params in model is not possible. Params is something which is made to exist only at controller and view level.
So best way is to write some helper method in controller to perform this.
Class Mycontroller < ApplicationController
before_action fetch_data, :only => [:index]
def fetch_data
#data = Model.find(params[:id])#use params to use fetch data from db
end
def index
end

Rails as_json with two unrelated models

Given two models and a controller:
Apples
class Apples < ActiveRecord::Base
belongs_to :not_oranges
...
def as_json(options={})
opts = {:include => [:not_oranges]}
super(options.reverse_merge! opts)
end
end
Oranges
class Oranges < ActiveRecord::Base
belongs_to :not_apples
...
def as_json(options={})
opts = {:include => [:not_apples]}
super(options.reverse_merge! opts)
end
end
Search Controller
class SearchController < ApplicationController
a = Apples.search params[:q]
o - Oranges.search params[:q]
#results = {
:apples => a,
:oranges => o
}
respond_to do |format|
format.json { render :json => #results }
end
As you can see, the two models are completely unrelated and both have different :include options in their as_json definitions.
All works as expected if the search query only hits apples or only hits oranges, but once both objects aren't empty I get:
undefined method `not_apples' for #<Oranges:0x00000004af8cd8>
Seems either the two as_json definitions are being merged, or Oranges.as_json is being overriden by Apples.as_json.
Is this expected behaviour? Is there any clean way around it without using something like RABL? I feel it would be overkill for my needs.
In pseudo code the code for hash as_json method looks like
def as_json(options={})
Hash[collect {|key,element| [key.to_s,element.as_json(options)]}]
end
But your element is modifying the options argument you pass to it. Hash is unaware of this and so passes the modified options hash to as json.
It's usually a good idea not to modify in place the arguments passed to you, except when it is very clear this is ok. I'd rewrite your method as
def as_json(options={})
defaults = {:include => :not_apples}
super(defaults.merge(options))
end

How do I use an instance variable assigned in a helper_method from a helper?

I've been drying up one of our controllers in our rails 2.3 app, and I've run up against a problem using an instance variable assigned in a helper_method. Originally, the situation was like this:
home_controller.rb:
class HomeController < ActionController::Base
def index
end
def popular
#popular_questions = PopularQuestion.paginate :page => params[:page],
<some complex query>
end
end
home_helper.rb:
module HomeHelper
def render_popular_questions
#popular_questions = PopularQuestion.paginate :page => 1,
<some complex query>
render :partial => 'popular'
end
end
home/index.html.haml
-cached do
.popular=render_popular_questions
home/popular.html.haml
=render :partial => 'popular'
home/_popular.html.haml
-if #popular_questions.length > 0
<show stuff>
hitting either / or /popular showed the appropriate box of popular questions.
Now, since the query was pretty much duplicated, and since paginate will use the correct page by default, I refactored this as:
home_controller.rb:
class HomeController < ActionController::Base
helper_method :get_popular_questions
def index
end
def popular
get_popular_questions
end
private
def get_popular_questions
#popular_questions = PopularQuestion.paginate :page => params[:page],
<some complex query>
end
end
home_helper.rb:
module HomeHelper
def render_popular_questions
get_popular_questions
render :partial => 'popular'
end
end
now when I go to /, I get
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.length
being raised in line 1 of home/_popular.html.haml
It seems that variables set from within helper_methods called from within helpers aren't accessible to the template. Have I made a mistake somewhere? If not, how do I use an instance variable assigned in a helper_method from a helper?
Pass them as parameters and local-variables:
home_controller.rb:
class HomeController < ActionController::Base
helper_method :get_popular_questions
def index
end
def popular
#popular_questions = get_popular_questions
end
private
def get_popular_questions
# remember that the final statement of a method is also the return-value
PopularQuestion.paginate :page => params[:page],
<some complex query>
end
end
home_helper.rb:
module HomeHelper
def render_popular_questions
questions = get_popular_questions
render :partial => 'popular', :locals => {:questions => questions}
end
end
now in your partial, use "questions" instead of "#popular_questions"
Just make sure that the main template for "popular" also need to populate this local variable too.

Resources