Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
3 total  XML / RSS feed 

acts_as_state_machine model

// ActiveRecord plugin for managing state changes
// elitists.textdriven.com/svn/plugins/acts_as_state_machine/

class Something < ActiveRecord::Base

  # Validations
  validate :validate_state_change

  # State Machine States
  acts_as_state_machine :initial => :new
  state :new
  state :enabled,   :after => :after_enabled
  state :disabled,  :after => :after_disabled

  # State Machine Events
  event :enabled do
    transitions :from => :new,      :to => :enabled
    transitions :from => :disabled, :to => :enabled
  end
  
  event :disabled do
    transitions :from => :new,      :to => :disabled
    transitions :from => :enabled,  :to => :disabled
  end

  # Instance Methods
  def validate_state_change
    return if new_record?
    old = self.class.find(id)
    old_state = old.state
    new_state = self.state
    self.state = old_state
    if old_state != new_state
      begin
        if self.method("#{new_state}!").call != true
          errors.add(:state, "cannot transition from #{old_state} to #{new_state}")
        end
      rescue NameError
      end
      self.state = new_state
    end
  end
end

US States Pop-up

Simply a pop-up form element of all 50 US States. Save you creating your own.

            <select name="state">
              <option value="" selected="selected">Select a state...option>
              /option>
              <option value="AK">Alaskaoption>
              /option>
              <option value="AR">Arkansasoption>
              /option>
              <option value="CO">Coloradooption>
              /option>
              <option value="DC">District of Columbiaoption>
              /option>
              <option value="FL">Floridaoption>
              /option>
              <option value="HI">Hawaiioption>
              /option>
              <option value="IL">Illinoisoption>
              /option>
              <option value="IA">Iowaoption>
              /option>
              <option value="KY">Kentuckyoption>
              /option>
              <option value="ME">Maineoption>
              /option>
              <option value="MA">Massachusettsoption>
              /option>
              <option value="MN">Minnesotaoption>
              /option>
              <option value="MO">Missourioption>
              /option>
              <option value="NE">Nebraskaoption>
              /option>
              <option value="NH">New Hampshireoption>
              /option>
              <option value="NM">New Mexicooption>
              /option>
              <option value="NC">North Carolinaoption>
              /option>
              <option value="OH">Ohiooption>
              /option>
              <option value="OR">Oregonoption>
              /option>
              <option value="RI">Rhode Islandoption>
              /option>
              <option value="SD">South Dakotaoption>
              /option>
              <option value="TX">Texasoption>
              /option>
              <option value="VT">Vermontoption>
              /option>
              <option value="WA">Washingtonoption>
              /option>
              <option value="WI">Wisconsinoption>
              /option>
            select>

State name to 2 letter code

Simple one-trick-pony PHP function to take a state name (case insensitive) and return the 2 letter abbreviation.

        function state_to_twoletter( $state_name ) {
                
                $state = array();
                $state['ALABAMA']='AL';
                $state['ALASKA']='AK';
                $state['AMERICAN SAMOA']='AS';
                $state['ARIZONA']='AZ';
                $state['ARKANSAS']='AR';
                $state['CALIFORNIA']='CA';
                $state['COLORADO']='CO';
                $state['CONNECTICUT']='CT';
                $state['DELAWARE']='DE';
                $state['DISTRICT OF COLUMBIA']='DC';
                $state['FEDERATED STATES OF MICRONESIA']='FM';
                $state['FLORIDA']='FL';
                $state['GEORGIA']='GA';
                $state['GUAM']='GU';
                $state['HAWAII']='HI';
                $state['IDAHO']='ID';
                $state['ILLINOIS']='IL';
                $state['INDIANA']='IN';
                $state['IOWA']='IA';
                $state['KANSAS']='KS';
                $state['KENTUCKY']='KY';
                $state['LOUISIANA']='LA';
                $state['MAINE']='ME';
                $state['MARSHALL ISLANDS']='MH';
                $state['MARYLAND']='MD';
                $state['MASSACHUSETTS']='MA';
                $state['MICHIGAN']='MI';
                $state['MINNESOTA']='MN';
                $state['MISSISSIPPI']='MS';
                $state['MISSOURI']='MO';
                $state['MONTANA']='MT';
                $state['NEBRASKA']='NE';
                $state['NEVADA']='NV';
                $state['NEW HAMPSHIRE']='NH';
                $state['NEW JERSEY']='NJ';
                $state['NEW MEXICO']='NM';
                $state['NEW YORK']='NY';
                $state['NORTH CAROLINA']='NC';
                $state['NORTH DAKOTA']='ND';
                $state['NORTHERN MARIANA ISLANDS']='MP';
                $state['OHIO']='OH';
                $state['OKLAHOMA']='OK';
                $state['OREGON']='OR';
                $state['PALAU']='PW';
                $state['PENNSYLVANIA']='PA';
                $state['PUERTO RICO']='PR';
                $state['RHODE ISLAND']='RI';
                $state['SOUTH CAROLINA']='SC';
                $state['SOUTH DAKOTA']='SD';
                $state['TENNESSEE']='TN';
                $state['TEXAS']='TX';
                $state['UTAH']='UT';
                $state['VERMONT']='VT';
                $state['VIRGIN ISLANDS']='VI';
                $state['VIRGINIA']='VA';
                $state['WASHINGTON']='WA';
                $state['WEST VIRGINIA']='WV';
                $state['WISCONSIN']='WI';
                $state['WYOMING']='WY';

                // Canadian Provinces
                // edited 12-5-07
                $state['ALBERTA']='AB';
                $state['BRITISH COLUMBIA']='BC';
                $state['MANITOBA']='MB';
                $state['NEW BRUNSWICK']='NB';
                $state['LABRADOR']='NL';
                $state['NEWFOUNDLAND]='NL';
                $state['NORTHWEST TERRITORIES']='NT';
                $state['NOVA SCOTIA']='NS';
                $state['NUNAVUT']='NU';
                $state['ONTARIO']='ON';
                $state['PRINCE EDWARD ISLAND']='PE';
                $state['QUEBEC']='QC';
                $state['SASKATCHEWAN']='SK';
                $state['YUKON']='YT';

                return $state[strtoupper( $state_name )]; 
                
        }

« Newer Snippets
Older Snippets »
3 total  XML / RSS feed