Sha256: 271b0e13ba6abe5017680739e9d76f9e8e403055c092d8bb2382306735236f35

Contents?: true

Size: 1.94 KB

Versions: 6

Compression:

Stored size: 1.94 KB

Contents

module Listings
  # = Helpers
  module ActionViewExtensions
    def render_listing(key, options = {})
      options.reverse_merge! :params => {}
      _params = Rails::VERSION::MAJOR < 5 ? params : params.to_unsafe_h
      params_for_listing = {:listing => key}.merge(_params).merge(options[:params]).with_indifferent_access
      listing = prepare_listing(params_for_listing, self)
      res = listings_partial_render 'listing', listing
      Kaminari::Helpers::Tag.paginate_with_listings(nil)
      res
    end

    def listings_link_to_filter(text, key, value)
      link_to text, '#', onclick: "$(this).closest('.listing').trigger('listings:filter:key:set', ['#{key}', '#{escape_javascript(value)}']);return false;"
    end

    def listings_partial_render(view, listing, options = {})
      prefix = [
        "listings/#{listing.name}",
        "listings/#{listing.theme}",
        "listings"
      ].select { |p| lookup_context.exists?("#{p}/_#{view}") }.first

      render "#{prefix}/#{view}", options.merge(listing: listing)
    end

    def prepare_listing(params, view_context, paging = true)
      params.delete :controller
      params.delete :action

      Kaminari::Helpers::Tag.paginate_with_listings(view_context.listings)

      listing_class = lookup_listing_class(params[:listing])
      listing_class.new.tap do |listing|
        _prepare_view_context view_context
        listing.view_context = view_context
        if !paging
          listing.page_size = :none
        end
        listing.query_items(params.respond_to?(:to_unsafe_h) ? params.to_unsafe_h : params)
      end
    end

    def lookup_listing_class(name)
      "#{name}_listing".classify.constantize
    end

    def _prepare_view_context(view_context)
      # forward methods from this view context to main app if they are not found
      view_context.class.send(:define_method, 'method_missing') do |m, *args, &block|
        view_context.main_app.send(m, *args, &block)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
listings-0.1.15 lib/listings/action_view_extensions.rb
listings-0.1.14 lib/listings/action_view_extensions.rb
listings-0.1.13 lib/listings/action_view_extensions.rb
listings-0.1.12 lib/listings/action_view_extensions.rb
listings-0.1.11 lib/listings/action_view_extensions.rb
listings-0.1.10 lib/listings/action_view_extensions.rb