Project

primary

0.01
No release in over 3 years
Low commit activity in last 3 years
This simple gem will help you with keeping default or primary record (one record that is marked as primary) - ie. default domain or language for site
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 3.0.0
 Project Readme

#Primary travis-ci

primary is simple gem/plugin that will help you ensure, that you have only one primary / default record in DB. Ie. your site can have multiple languages (stored in db), but only one of them is primary / default.

Instalation

Simply add gem 'primary' to your Gemfile.

After running bundle install you can mark model as acting as primary / default.

Usage

To your model (ie. Language) add is_primary column and declare models as beeing primary by adding is_primary declaration:

class Language < ActiveRecord::Base
  is_primary
  
  attr_accessible :language, :is_primary
end

###Sample

Language.count
# => 0

lang_en = Language.create({:language=>'en'})
lang_en.is_primary
# => true

lang_de = Language.create({:language=>'de'})
lang_de.is_primary
# => false

lang_de.is_primary = true
lang_de.save
lang_de.is_primary
# => true

lang_en.reload
lang_en.is_primary
# => false

###Possible options

is_primary :on=>'is_default', :scope=>'site_id'

:on     => 'is_default'   # use different column name
:scope  => 'site_id'      # scope default to different column - ie. you'll
                          # be able to define multiple languages per multiple
                          # sites and each site will have one default lang.
                          
                          # Scope can be defined as array of params
:scope => ['site_id', 'language_code']
:scope => ['polymorphic_id', 'polymorphic_type']
:auto_primary_record => false   # if you don't want primary record
                                # to be set automatically (default: true)

##Requirements

Currently gem requires Rails 3.2, I'll have to check, if it works as desired with lower versions of Ruby.

##License

Project is distributed under general terms of MIT-LICENSE.