Skip to content

Commit

Permalink
Adds time_zone and token to Site.
Browse files Browse the repository at this point in the history
Whenever a site is stored, a document is created
in the "sites" collection in MongoDB containing
the time zone.
  • Loading branch information
jcxplorer committed Mar 3, 2011
1 parent 5b2f556 commit b5bf470
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 2 deletions.
14 changes: 13 additions & 1 deletion app/models/site.rb
@@ -1,5 +1,17 @@
class Site < ActiveRecord::Base

validates_presence_of :name
validates_presence_of :name, :time_zone

before_create :create_mongo_site

def time_zone_id
ActiveSupport::TimeZone::MAPPING[time_zone]
end

private

def create_mongo_site
self.token = Mongo.db["sites"].insert({ :tz => time_zone_id }).to_s
end

end
1 change: 1 addition & 0 deletions app/views/sites/_form.html.haml
@@ -1,3 +1,4 @@
= simple_form_for @site do |f|
= f.input :name, :label => "Site name"
= f.input :time_zone
= f.button :submit, "Save"
9 changes: 9 additions & 0 deletions db/migrate/20110228121355_add_token_to_sites.rb
@@ -0,0 +1,9 @@
class AddTokenToSites < ActiveRecord::Migration
def self.up
add_column :sites, :token, :string
end

def self.down
remove_column :sites, :token
end
end
9 changes: 9 additions & 0 deletions db/migrate/20110303181824_add_time_zone_to_sites.rb
@@ -0,0 +1,9 @@
class AddTimeZoneToSites < ActiveRecord::Migration
def self.up
add_column :sites, :time_zone, :string
end

def self.down
remove_column :sites, :time_zone
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Expand Up @@ -10,12 +10,14 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20110219100357) do
ActiveRecord::Schema.define(:version => 20110303181824) do

create_table "sites", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.string "token"
t.string "time_zone"
end

create_table "users", :force => true do |t|
Expand Down
1 change: 1 addition & 0 deletions spec/acceptance/sites_spec.rb
Expand Up @@ -32,6 +32,7 @@
page.should have_active_navigation("Sites")

fill_in "Site name", :with => "Snowfinch info"
select "Helsinki", :from => "Time zone"
click_button "Save"

page.should have_notice('"Snowfinch info" has been created.')
Expand Down
1 change: 1 addition & 0 deletions spec/factories.rb
Expand Up @@ -11,6 +11,7 @@

factory :site do
name "Snowfinch"
time_zone "Helsinki"
end

end
7 changes: 7 additions & 0 deletions spec/models/site_spec.rb
Expand Up @@ -4,4 +4,11 @@

it { should validate_presence_of(:name) }

describe "#time_zone_id" do
it "returns the TZInfo indentifier" do
site = Site.new(:time_zone => "Helsinki")
site.time_zone_id.should == "Europe/Helsinki"
end
end

end

0 comments on commit b5bf470

Please sign in to comment.