Skip to content

NOT ACTIVELY MAINTAINED Rails adapter for Carmen (provides country_select and subregion_select)

License

Notifications You must be signed in to change notification settings

Studiosity/carmen-rails

This branch is 19 commits ahead of carmen-ruby/carmen-rails:master.

Folders and files

NameName
Last commit message
Last commit date
Jan 13, 2025
Jan 13, 2025
Dec 21, 2023
Dec 21, 2023
Nov 3, 2019
Nov 3, 2019
Mar 6, 2014
Jan 13, 2025
May 9, 2012
May 12, 2023
Dec 21, 2023
Dec 21, 2023

Repository files navigation

carmen-rails

carmen-rails is a Rails 5-7 plugin that supplies two new form helper methods: country_select and subregion_select. It uses carmen as its source of geographic data.

Requirements

carmen-rails requires Ruby 2.7 or greater (although might work with older versions)

Installation

Just add carmen-rails to your Gemfile:

gem 'carmen-rails', '~> 1.0.0'

Usage

<%= form_for(@order) do |f| %>
  <div class="field">
    <%= f.label :country_code %><br />
    <%= f.country_select :country_code, {priority: %w(US CA), prompt: 'Please select a country'} %>
  </div>
<% end %>

SimpleForm

Pass the object to the country_select helper. This ensures the persisted country is selected when the form is rendered.

<%= simple_form_for @user do |f| %>
  <%= f.input :country_code do %>
    <%= f.country_select :country_code, {object: f.object, prompt: 'Country'} %>
  <% end %>
<% end %>

Passing the object is necessary when using nested form fields with Formtastic.

How do I only display a subset of countries/regions?

Carmen had a concept of excluded countries in the old days, where you could specify certain countries or regions to not include in a select.

The new (and much more flexible) way to handle this is to write a helper method that returns the subset of regions you want to provide:

def only_us_and_canada
  Carmen::Country.all.select{|c| %w{US CA}.include?(c.code)}
end

And then in your form something like this:

<%= f.select :country, region_options_for_select(only_us_and_canada) %>

More docs coming soon. In the meantime, all of the public methods in carmen-rails have been thoroughly TomDoc'ed.

Demo app

There is a live demo app that shows carmen-rails in action, and includes a step-by-step setup guide.

Configuration

Using this library will automatically set Carmen to use Rails' built-in I18n functionality. This means that changing some configuration should be done through Rails and not Carmen. For example, adding paths for additional locale files should be done inside config/application.rb:

config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]

About

NOT ACTIVELY MAINTAINED Rails adapter for Carmen (provides country_select and subregion_select)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%