vijay
vijay

Reputation: 31

How to manage project-wide constants in a project using ROR?

lib/constant.rb

module Constant

  BANQUET_TYPE_OF_OFFER = [['Narrow By Offer Type',''], ["A la Carte", "A la Carte"],

  ["Alcohol Offer", "Alcohol Offer"], ["Buffet", "Buffet"], ["Brunch", "Brunch"], 

  ["Happy Hours", "Happy Hours"], ["Set Menu", "Set Menu"],["Banquets", "Banquets" ]]

end

application_controller.rb

class ApplicationController < ActionController::Base

    include Constant

end

views/banquets/banquet.html.haml

= f.select_tag "type_of_offer", options_for_select(Constant::BANQUET_TYPE_OF_OFFER, 
@selected), :name => "banquet[type_of_offer][]", :multiple => true

I created many constants in rails application like java constants. This is best practice or not ??? Any other solution is there?

Upvotes: 1

Views: 235

Answers (1)

Federico Builes
Federico Builes

Reputation: 5097

As pointed out by @Batkins, you don't need to include the module in your class.

When it comes down to constants, it boils down to preferences on using constants defined as Ruby vs. defined as YAML.

Ideally the BANQUET_TYPE_OF_OFFER would be defined in the model where it makes more sense and not in a global location. If you definitely have to have it outside your models, think about moving it into config/initializers/ instead of lib.

Upvotes: 2

Related Questions