From 4d9569f580b289a888450f8e0cfc0343c4f393c0 Mon Sep 17 00:00:00 2001 From: Gabriel Sobrinho Date: Sun, 24 Jan 2010 17:35:41 -0200 Subject: [PATCH] admin column should be false by default, update models and update specs --- app/models/meeting.rb | 7 +-- app/models/notifier.rb | 6 +-- app/models/project.rb | 7 ++- app/models/user.rb | 38 +++++++------- app/models/user_session.rb | 2 +- ...24192406_change_default_value_for_admin.rb | 10 ++++ spec/models/meeting_spec.rb | 51 +++++++------------ spec/models/project_spec.rb | 29 ++++++----- spec/models/user_spec.rb | 46 +++++++++-------- 9 files changed, 101 insertions(+), 95 deletions(-) create mode 100644 db/migrate/20100124192406_change_default_value_for_admin.rb diff --git a/app/models/meeting.rb b/app/models/meeting.rb index 561b797..4d85e92 100644 --- a/app/models/meeting.rb +++ b/app/models/meeting.rb @@ -1,15 +1,16 @@ class Meeting < ActiveRecord::Base URL_REGEXP = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix + # associations belongs_to :user - # Upload + # plugins has_attached_file :slide_preview, :styles => { :medium => "267x345>", :thumb => "116x150>" } - # Scopes + # scopes default_scope :order => 'start_on DESC' - # Validations + # validations validates_presence_of :name, :description with_options :allow_blank => true do |m| diff --git a/app/models/notifier.rb b/app/models/notifier.rb index 0be4972..9b1d63d 100644 --- a/app/models/notifier.rb +++ b/app/models/notifier.rb @@ -1,8 +1,8 @@ class Notifier < ActionMailer::Base def signup_confirmation user recipients user.email_with_name - subject 'RailsMG # Confirmação de conta' - from 'donotreply@railsmg.com.br' + subject 'RailsMG - Confirmação de conta' + from 'donotreply@railsmg.org' body :user => user end -end \ No newline at end of file +end diff --git a/app/models/project.rb b/app/models/project.rb index c9bf073..36bd637 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1,11 +1,11 @@ class Project < ActiveRecord::Base - # Associations + # associations belongs_to :user, :counter_cache => true - # Scopes + # scopes default_scope :order => 'projects.name' - # Validations + # validations validates_presence_of :user, :name with_options :allow_blank => true do |u| @@ -21,4 +21,3 @@ def github_url "http://github.com/#{user.github}/#{name}" end end - diff --git a/app/models/user.rb b/app/models/user.rb index 7336944..e2f971b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,7 +1,7 @@ require 'open-uri' class User < ActiveRecord::Base - # Authlogic + # plugins acts_as_authentic do |c| c.validate_email_field = false c.validate_login_field = false @@ -9,20 +9,20 @@ class User < ActiveRecord::Base c.perishable_token_valid_for = 48.hours end - # Associations + # associations has_many :projects has_many :meetings - # Scopes + # scopes default_scope :order => 'users.name' named_scope :with_projects, :include => :projects, :order => 'users.name, projects.name' named_scope :active, :conditions => { :active => true } - # Attributes - attr_protected :active + # attributes + attr_protected :active, :admin - # Validations + # validations validates_presence_of :name, :email, :city validates_length_of :password, :minimum => 6, :if => :require_password? validates_confirmation_of :password, :if => :require_password? @@ -35,21 +35,22 @@ class User < ActiveRecord::Base end after_save :fetch_projects! - # Fetch user projects using YAML API from Github + + # Fetch user projects from Github def fetch_projects! - self.projects.destroy_all # Destroy current user projects + self.projects.destroy_all - unless github.blank? - result = YAML.load open("http://github.com/api/v2/yaml/repos/show/#{github}/") + returning true do + if self.github? + result = YAML.load open("http://github.com/api/v2/yaml/repos/show/#{github}/") - result['repositories'].each do |repository| - self.projects.create! :name => repository[:name], :description => repository[:description] + result['repositories'].each do |repository| + self.projects.create! :name => repository[:name], :description => repository[:description] + end end - - true end - rescue OpenURI::HTTPError - false # user not found, ignore + rescue OpenURI::HTTPError # user not found, ignore + false end after_create :deliver_signup_confirmation @@ -75,9 +76,8 @@ def email_with_name end def self.active! perishable_token - find_using_perishable_token!(perishable_token).tap do |user| + returning find_using_perishable_token!(perishable_token) do |user| user.update_attribute(:active, true) if user end end - -end \ No newline at end of file +end diff --git a/app/models/user_session.rb b/app/models/user_session.rb index 8c19d19..93642e5 100644 --- a/app/models/user_session.rb +++ b/app/models/user_session.rb @@ -1,2 +1,2 @@ class UserSession < Authlogic::Session::Base -end \ No newline at end of file +end diff --git a/db/migrate/20100124192406_change_default_value_for_admin.rb b/db/migrate/20100124192406_change_default_value_for_admin.rb new file mode 100644 index 0000000..6bdc9e0 --- /dev/null +++ b/db/migrate/20100124192406_change_default_value_for_admin.rb @@ -0,0 +1,10 @@ +class ChangeDefaultValueForAdmin < ActiveRecord::Migration + def self.up + change_column_default :users, :admin, false + end + + def self.down + raise ActiveRecord::IrreversibleMigration + end +end + diff --git a/spec/models/meeting_spec.rb b/spec/models/meeting_spec.rb index 1b36271..5b36b65 100644 --- a/spec/models/meeting_spec.rb +++ b/spec/models/meeting_spec.rb @@ -1,48 +1,35 @@ require 'spec_helper' describe Meeting do - before do - @meeting = Meeting.make - end - - # Upload - should_have_attached_file(:slide_preview, :styles => { :medium => "267x345>", :thumb => "116x150>" }) - - # Database - should_have_column :name, :type => :string + # database + should_have_columns :name, :video, :code, :slides, :type => :string should_have_column :description, :type => :text + should_have_column :start_on, :type => :date - # Associations + # asssociations should_belong_to :user - - # Scopes + + # plugins + should_have_attached_file :slide_preview, :styles => { :medium => "267x345>", :thumb => "116x150>" } + + # scopes should_have_default_scope :order => 'start_on DESC' - # Validations + # validations should_validate_presence_of :name, :description - - # FIXME: Find a way to be more DRY - describe "when seting a url" do - before :all do - @good_urls = "http://slideshare.net/slide1", "http://github.com/danielvlopes" - @bad_urls = "slideshare.net", "http://github/danielvlopes" - end - - it { should allow_values_for :slides, @good_urls } - it { should_not allow_values_for :slides, @bad_urls } - it { should allow_values_for :video, @good_urls } - it { should_not allow_values_for :video, @bad_urls } - it { should allow_values_for :code, @good_urls } - it { should_not allow_values_for :code, @bad_urls } - end with_options :allow_blank => true do |m| m.should_validate_length_of :name, :in => 1..255 - m.should_validate_uniqueness_of :name, :scope => :user_id, :case_sensitive => false + # TODO: remarkable fix: m.should_validate_uniqueness_of :name, :scope => :user_id, :case_sensitive => false + end + + %w(slides video code).each do |attribute| + should_allow_values_for attribute, "http://slideshare.net/slide1", "http://github.com/danielvlopes" + should_not_allow_values_for attribute, "slideshare.net", "http://github/danielvlopes" end it 'to_s should return name' do - @meeting.name.should be_eql(@meeting.name) + subject.name = 'Nome da Palestra' + subject.to_s.should eql 'Nome da Palestra' end - -end \ No newline at end of file +end diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 3067045..dcca361 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -1,31 +1,34 @@ require 'spec_helper' describe Project do - before do - @project = Project.make - end - - # Database + # database should_have_columns :name, :description, :type => :string should_have_column :user_id, :type => :integer should_have_index :user_id - # Scopes - should_have_default_scope :order => 'projects.name' - - # Associations + # associations should_belong_to :user, :counter_cache => true - # Validations + # scopes + should_have_default_scope :order => 'projects.name' + + # validations should_validate_presence_of :user, :name with_options :allow_blank => true do |p| p.should_validate_length_of :name, :description, :in => 1..255 - p.should_validate_uniqueness_of :name, :scope => :user_id, :case_sensitive => false + # TODO: remarkable fix: p.should_validate_uniqueness_of :name, :scope => :user_id, :case_sensitive => false end it 'to_s should return name' do - @project.to_s.should be_eql(@project.name) + subject.name = 'Nome do Projeto' + subject.to_s.should eql 'Nome do Projeto' end -end + it 'github_url should return a valid github url' do + subject.user = mock_model(User, :github => 'sobrinho') + subject.name = 'projeto' + + subject.github_url.should eql 'http://github.com/sobrinho/projeto' + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 6419d83..6f3f2a4 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,52 +1,58 @@ require 'spec_helper' describe User do - before do - @user = User.make - end + # database + should_have_columns :name, :github, :email, :crypted_password, :password_salt, :persistence_token, + :perishable_token, :city, :site, :twitter, :type => :string + should_have_column :projects_count, :default => 0, :type => :integer + should_have_column :using_ruby_since, :type => :integer + should_have_column :about, :type => :text + should_have_columns :public_email, :active, :admin, :type => :boolean, :default => false - # Database - should_have_columns :name, :github, :email, :crypted_password, :password_salt, - :persistence_token, :perishable_token, :city, :type => :string - should_have_column :projects_count, :type => :integer, :default => 0 should_have_indices :email, :persistence_token, :unique => true - # Scopes + # scopes should_have_default_scope :order => 'users.name' should_have_named_scope :with_projects, :include => :projects, :order => 'users.name, projects.name' should_have_named_scope :active, :conditions => { :active => true } - # Associations + # associations should_have_many :projects, :meetings - # Validations - should_validate_presence_of :name, :email + # validations + should_validate_presence_of :name, :email, :city should_validate_length_of :password, :minimum => 6, :if => :require_password? should_validate_confirmation_of :password, :if => :require_password? with_options :allow_blank => true do |u| u.should_validate_length_of :name, :city, :github, :in => 1..255 - # FIXME: u.should_validate_as_email :email - u.should_validate_uniqueness_of :email, :case_sensitive => false - # FIXME: u.should_validate_uniqueness_of :github, :case_sensitive => false + # TODO: remarkable fix: u.should_validate_uniqueness_of :email, :case_sensitive => false + # TODO: remarkable fix: u.should_validate_uniqueness_of :github, :case_sensitive => false end + should_allow_values_for :email, 'john@doe.com', 'john.doe@gmail.ru' + should_not_allow_values_for :email, 'john@doe', 'john.doe' + should_allow_values_for :github, 'john', 'jonh_doe' should_not_allow_values_for :github, 'jonhn doe', 'john.doe' it 'active should be on protected attributes' do - User.protected_attributes.should be_include 'active' + User.protected_attributes.should include 'active' + User.protected_attributes.should include 'admin' end it 'to_s should return name' do - @user.to_s.should be_eql(@user.name) + subject.name = 'Gabriel Sobrinho' + subject.to_s.should eql 'Gabriel Sobrinho' end it 'active! should set active as true' do - User.active!(@user.perishable_token).should be_eql @user - @user.reload - @user.active.should be_true + user = User.make + + User.active!(user.perishable_token).should eql user + + user.reload + user.active.should be true end end -