Monday, 15 February 2010

Rails 4 helper class -



Rails 4 helper class -

i'm struggling find best way implement next class in rails 4 application. need utilize class in 1 controller , it's lengthy.

class yelp ## # performs query on yelp search or business api ## def query(business, term = 'lunch', cuisine = 'restaurants', limit = restaurant_limit, location = get_city) # .. end ## # extracts list of cuisines formatted view # @param array of restaurant objects # @return hash of cuisines ## def get_cuisines(restaurants) # .. end ## # creates [non-activerecord] model objects array of restaurant info # @param array of hashes containing restaurant info # @return array of objects containing restaurant info ## def parse_restaurants(restaurants) # .. end ## # creates gmaps pins restaurant objects # @param array of restaurant objects become pins # @return gmaps pins ## def get_pins(restaurants) # .. end end

i've looked helper modules understand view logic.

i'm hesitant set logic application_controller.rb because, said, utilize in 1 of controllers.

i've tried putting class in lib directory had no success. followed this post maintain getting: undefined method 'my_method' <maincontroller>.

create module activesupport::concern in app/controllers/concerns/ directory, let's phone call yelp_searcher.rb:

module yelpsearcher extend activesupport::concern ## # performs query on yelp search or business api ## def query(business, term = 'lunch', cuisine = 'restaurants', limit = restaurant_limit, location = get_city) # .. end ## # extracts list of cuisines formatted view # @param array of restaurant objects # @return hash of cuisines ## def get_cuisines(restaurants) # .. end ## # creates [non-activerecord] model objects array of restaurant info # @param array of hashes containing restaurant info # @return array of objects containing restaurant info ## def parse_restaurants(restaurants) # .. end ## # creates gmaps pins restaurant objects # @param array of restaurant objects become pins # @return gmaps pins ## def get_pins(restaurants) # .. end end

use in thatonecontroller:

class thatonecontroller < applicationcontroller include yelpsearcher # more code here.. end

more reading on topic here.

ruby-on-rails

No comments:

Post a Comment