Tag: Rails 4.0

Rails 4.0: ActionView::Template::Error: undefined method paginate with Kaminari

Refactoring can be sometimes a pain in the ass. Especially when you're refactoring things that are really old. Today I was cleaning an almost 5 years old piece of code. It was a Rails 4.0 (migrated many times from 1.2) app that had helpers auto-loading turned on. I hated this, because it forced us to use names with prefixes for many methods.

Disabling it was really easy (in config/application.rb):

config.action_controller.include_all_helpers = false

Of course after that you need to require all the helpers that you use:

helper ApplicationHelper
helper FormHelper

Everything was fine except one small detail:

ActionView::Template::Error: undefined method `paginate' for #<#<Class:0x0000000929a3c0>:0x0000000aa2c5b0>
    app/views/admin/module/urls/index.html.haml:1:in `_app_views_admin_module_urls_index_html_haml__883223636925488268_77061720'
    app/controllers/application_controller.rb:57:in `respond_with'
    app/controllers/admin/module/urls_controller.rb:9:in `index'
    test/controllers/admin/module/urls_controller_test.rb:103:in `block in <class:UrlsControllerTest>'

Kaminari stopped working. It seems that it was loaded without issues, except the helper. To fix this, just include in you application controller following code:

helper KaminariHelper

How to use Ransack helpers with MongoDB and Mongoid on Rails 4.0

Ransack is a rewrite of MetaSearch. It allows you to create search forms and sorting for your ActiveRecord data. Somehow I really miss that, when working with Mongoid. To be honest I can even live without the whole Ransack search engine, but I liked the UI helpers for search forms and sorting data.

I've really wanted to be able to do something like that:

@users = current_app.users.order(params[:q]).page(current_page)

and

%tr.condensed{:role => "row"}
  %th= sort_link @search, :guid, 'User id'
  %th= sort_link @search, :email, 'Email address'
  %th= sort_link @search, :current_country, 'Country'
  %th= sort_link @search, :current_city, 'City'

with Mongoid documents. It turns out, that if you don't need really sophisticated stuff, you can use Ransack helpers even with Mongoid. In order to use search/sort UI features, you need @search Ransack model instance. It is based on ActiveRecord data and unfortunately we don't have any models that would suit our needs. That's why we need to create a dummy one:

module System
  # In order to use Ransack helpers and engine for non AR models, we need to 
  # initialize @search with AR instance. To prevent searching (this is just a stub)
  # we do it usign ActiveRecord dummy class that is just a blank class
  class ActiveRecord < ActiveRecord::Base

    def self.columns
      @columns ||= []
    end

  end
end

This is enough to use it in controllers and views:

# In order to use Ransack helpers and engine for non AR models, we need to 
# initialize @search with AR instance. To prevent searching (this is just a stub)
# we do it with none
def search
  @search ||= ::System::ActiveRecord.none.search(params[:q])
end

It is a dummy ActiveRecord class, that will always return empty scope (none), but it is more than enough to allow us to work with UI helpers. To handle ordering of Mongoid document collection, we could use following code:

module System
  module Mongoid
      # Order engine used to order mongoid data
      # It is encapsulated in segment module, because we store here all the
      # things that are related to filtering/ordering data
      #
      # Since it returns the resource/scope that was provided, modified
      # accordingly to sorting rules, this class can be used in defined
      # scopes and it will work with scope chainings
    class Orderable
      # @param resource [Mongoid::Criteria] Mongoid class or a subset 
      #   of it (based on Mongoid::Criteria)
      # @param rules [Hash] hash with rules for ordering
      # @return [Mongoid::Criteria] mongoid criteria scope
      # @example
      #   System::Mongoid::Orderable.new(current_app.users, params[:q])
      def initialize(resource, rules = {})
        @rules = rules
        @resource = resource
      end

      # Applies given sort order if there is one.
      # If not, it will do nothing
      # @example Ordered example scope
      #   scope :order, -> order { System::Mongoid::Orderable.new(self, order).apply }
      def apply
        order unless @rules.blank?
        @resource.criteria
      end

      private

      # Sets given order based on order rules (if there are any)
      # or it will do nothing if no valid order rules provided
      def order
        order = "#{@rules[:s]}".split(' ')
        @resource = order.blank? ? @resource : @resource.order_by(order)
      end
    end
  end
end

This can be used to create scopes like that:

class User
  include Mongoid::Document
  include Mongoid::Attributes::Dynamic

  # Scope used to set all the params from ransack order engine
  # @param [Hash, nil] hash with order options or nil if no options
  # @return [Mongoid::Criteria] scoping chain
  # @example
  #   User.order(params[:q]).to_a #=> order results
  scope :order, -> order { System::Mongoid::Orderable.new(self, order).apply }
end

Copyright © 2024 Closer to Code

Theme by Anders NorenUp ↑