Permalink
Browse files

* added Pop3 model

* added plugin to have ActiveRecord without Table
* setup_mailer to setup Net::POP3
  • Loading branch information...
1 parent 76f9ba6 commit b3f414c06c4ba6bfbdd1ec4df0635deba1e636f9 @hone committed Jan 8, 2009
View
42 app/models/pop3.rb
@@ -0,0 +1,42 @@
+require 'net/pop'
+
+class Pop3 < ActiveRecord::BaseWithoutTable
+ DEFAULT_PORT = 110
+ DEFAULT_SSL_PORT = 992
+
+ column :email_address, :string
+ column :server , :string
+ column :old_server, :string
+ column :username , :string
+ column :password , :string
+ column :ssl , :boolean
+ column :port , :integer
+ column :old_port, :integer
+
+ attr_reader :mailer
+
+ # callbacks don't seem to be working
+# before_save :setup_mailer
+
+ def setup_mailer
+ if self.ssl
+ self.port = DEFAULT_SSL_PORT if self.port.nil?
+ else
+ self.port = DEFAULT_PORT if self.port.nil?
+ end
+
+ if old_server != server or old_port != port
+ @mailer = Net::POP3.new( self.server, self.port )
+ end
+
+ if self.ssl
+ @mailer.enable_ssl
+ else
+ @mailer.disable_ssl
+ end
+
+ # keep track of old server and port
+ self.old_server = self.server
+ self.old_port = self.port
+ end
+end
View
3 app/models/remote_mail.rb
@@ -0,0 +1,3 @@
+# interface for accessing RemoteMail
+module RemoteMail
+end
View
7 spec/fixtures/pop3s.yml
@@ -0,0 +1,7 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+# one:
+# column: value
+#
+# two:
+# column: value
View
120 spec/models/pop3_spec.rb
@@ -0,0 +1,120 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+module Pop3SpecHelper
+ def setup_pop3( opts = {}, setup_mailer = true )
+ @valid_attributes = {
+ :email_address => 'test.otherinbox@gmail.com',
+ :server => 'pop.gmail.com',
+ :username => 'test.otherinbox@gmail.com',
+ :password => '0th3r1nb0x', # TODO need to encrypt this
+ :ssl => true,
+ :port => 995
+ }.merge( opts )
+
+ @pop3 = Pop3.new
+ @pop3.attributes = @valid_attributes
+ @pop3.setup_mailer if setup_mailer
+ end
+end
+
+describe Pop3, "setup mailer" do
+ include Pop3SpecHelper
+
+ before(:each) do
+ setup_pop3
+ end
+
+ it "should setup mailer" do
+ @pop3.mailer.class.should == Net::POP3
+ end
+
+ it "should set SSL to true" do
+ @pop3.mailer.use_ssl?.should be_true
+ end
+
+ it "should set SSL to false" do
+ @pop3.ssl = false
+ @pop3.setup_mailer
+
+ @pop3.mailer.use_ssl?.should be_false
+ end
+
+ it "should set default port if not set" do
+ setup_pop3( { :port => nil, :ssl => false } )
+
+ @pop3.port.should == Pop3::DEFAULT_PORT
+ end
+
+ it "should set default ssl port if not set" do
+ setup_pop3( :port => nil )
+
+ @pop3.port.should == Pop3::DEFAULT_SSL_PORT
+ end
+
+ it "should keep track of old server" do
+ setup_pop3( @valid_attributes, false )
+ @pop3.server.should == @valid_attributes[:server]
+ @pop3.old_server.should be_nil
+ @pop3.setup_mailer
+
+ new_server = "pop.dreamhost.com"
+ @pop3.server = new_server
+ @pop3.server.should == new_server
+ @pop3.old_server.should == @valid_attributes[:server]
+ @pop3.setup_mailer
+
+ @pop3.old_server.should == new_server
+ end
+
+ it "should kepe track of old port" do
+ setup_pop3( @valid_attributes, false )
+ @pop3.port.should == @valid_attributes[:port]
+ @pop3.old_port.should be_nil
+ @pop3.setup_mailer
+
+ new_port = 992
+ @pop3.port = new_port
+ @pop3.port.should == new_port
+ @pop3.old_port.should == @valid_attributes[:port]
+ @pop3.setup_mailer
+
+ @pop3.old_port.should == new_port
+ end
+
+ it "should not create a new Net::POP3 if server or port hasn't been changed" do
+ old_mailer = @pop3.mailer
+ @pop3.setup_mailer
+
+ old_mailer.should === @pop3.mailer
+ end
+
+ it "should create new Net::POP3 object upon server change" do
+ old_mailer = @pop3.mailer
+ @pop3.server = "pop.dreamhost.com"
+ @pop3.setup_mailer
+
+ old_mailer.should_not == @pop3.mailer
+ end
+
+ it "should create new Net::POP3 object upon port change" do
+ old_mailer = @pop3.mailer
+ @pop3.port = 992
+ @pop3.setup_mailer
+
+ old_mailer.should_not == @pop3.mailer
+ end
+
+end
+
+describe Pop3, "download mail" do
+ it "should not connect"
+
+ it "should download mail" do
+ pending "get setup mailer first"
+ @pop3.save
+ @pop3.mailer.mail.size.should == 3
+ end
+end
+
+describe Pop3, "validations" do
+end
View
1 vendor/plugins/active_record_without_table/.gitignore
@@ -0,0 +1 @@
+*.gem
View
8 vendor/plugins/active_record_without_table/CHANGELOG
@@ -0,0 +1,8 @@
+[03 July 2008]
+
+* Updated to work as a RubyGem
+
+
+[27 April 2007]
+
+* Correctly cache class instance variables containing column information [Reported by Nils Jonsson]
View
20 vendor/plugins/active_record_without_table/MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2006 Jonathan Viney
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
27 vendor/plugins/active_record_without_table/README
@@ -0,0 +1,27 @@
+= ActiveRecordBaseWithoutTable
+
+If you find this plugin useful, please consider a donation to show your support!
+
+ http://www.paypal.com/cgi-bin/webscr?cmd=_send-money
+
+ Email address: jonathan.viney@gmail.com
+
+== Instructions
+
+Get the power of ActiveRecord models, including validation, without having a table in the database.
+
+ class Contact < ActiveRecord::BaseWithoutTable
+ column :name, :string
+ column :email_address, :string
+ column :message, :text
+
+ validates_presence_of :name, :email_address, :string
+ end
+
+This model can be used just like a regular model based on a table, except it will never be saved to the database.
+
+There is a good blog post available on the plugin:
+
+ http://www.kangarooit.com/developer_blog/2007/02/email-form-validation-in-ruby-on-rails.php
+
+Any bugs, questions, comments please feel free to email me: jonathan.viney@gmail.com
View
22 vendor/plugins/active_record_without_table/Rakefile
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the active_record_base_without_table plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the active_record_base_without_table plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'ActiveRecordBaseWithoutTable'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
View
33 vendor/plugins/active_record_without_table/active_record_without_table.gemspec
@@ -0,0 +1,33 @@
+Gem::Specification.new do |s|
+ s.name = 'active_record_without_table'
+ s.version = '1.1'
+ s.date = '2008-07-05'
+
+ s.summary = "Allows creation of ActiveRecord models that work without any database backend"
+ s.description = "Get the power of ActiveRecord models, including validation, without having a table in the database."
+
+ s.authors = ['RailsJedi', 'Jonathan Viney']
+ s.email = 'railsjedi@gmail.com'
+ s.homepage = 'http://github.com/jcnetdev/active_record_without_table'
+
+ s.has_rdoc = true
+ s.rdoc_options = ["--main", "README"]
+ s.extra_rdoc_files = ["README"]
+
+ s.add_dependency 'activerecord', ['>= 2.0']
+
+ s.files = ["CHANGELOG",
+ "MIT-LICENSE",
+ "README",
+ "Rakefile",
+ "init.rb",
+ "rails/init.rb",
+ "active_record_without_table.gemspec",
+ "lib/active_record_without_table.rb",
+ "lib/active_record/base_without_table.rb"]
+
+ s.test_files = ["test/abstract_unit.rb",
+ "test/active_record_base_without_table_test.rb",
+ "test/database.yml"]
+
+end
View
1 vendor/plugins/active_record_without_table/init.rb
@@ -0,0 +1 @@
+require File.dirname(__FILE__) + "/rails/init"
View
26 vendor/plugins/active_record_without_table/lib/active_record/base_without_table.rb
@@ -0,0 +1,26 @@
+module ActiveRecord
+ class BaseWithoutTable < Base
+ self.abstract_class = true
+
+ def create_or_update
+ errors.empty?
+ end
+
+ class << self
+ def columns()
+ @columns ||= []
+ end
+
+ def column(name, sql_type = nil, default = nil, null = true)
+ columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
+ reset_column_information
+ end
+
+ # Do not reset @columns
+ def reset_column_information
+ generated_methods.each { |name| undef_method(name) }
+ @column_names = @columns_hash = @content_columns = @dynamic_methods_hash = @read_methods = nil
+ end
+ end
+ end
+end
View
0 vendor/plugins/active_record_without_table/lib/active_record_without_table.rb
No changes.
View
1 vendor/plugins/active_record_without_table/rails/init.rb
@@ -0,0 +1 @@
+# Plugin intialization code here...
View
15 vendor/plugins/active_record_without_table/test/abstract_unit.rb
@@ -0,0 +1,15 @@
+require 'test/unit'
+
+begin
+ require File.dirname(__FILE__) + '/../../../../config/boot'
+ require 'active_record'
+rescue LoadError
+ require 'rubygems'
+ require_gem 'activerecord'
+end
+
+require File.dirname(__FILE__) + '/../lib/active_record/base_without_table'
+
+config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
+ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log')
+ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'mysql'])
View
41 vendor/plugins/active_record_without_table/test/active_record_base_without_table_test.rb
@@ -0,0 +1,41 @@
+require File.dirname(__FILE__) + '/abstract_unit'
+
+class Person < ActiveRecord::BaseWithoutTable
+ column :name, :string
+ column :lucky_number, :integer, 4
+
+ validates_presence_of :name
+end
+
+class ActiveRecordBaseWithoutTableTest < Test::Unit::TestCase
+ def test_default_value
+ assert_equal 4, Person.new.lucky_number
+ end
+
+ def test_validation
+ p = Person.new
+
+ assert !p.save
+ assert p.errors[:name]
+
+ assert p.update_attributes(:name => 'Name')
+ end
+
+ def test_typecast
+ assert_equal 1, Person.new(:lucky_number => "1").lucky_number
+ end
+
+ def test_cached_column_variables_reset_when_column_defined
+ cached_variables = %w(column_names columns_hash content_columns dynamic_methods_hash read_methods)
+
+ Person.column_names
+ Person.columns_hash
+ Person.content_columns
+ Person.column_methods_hash
+ Person.read_methods
+
+ cached_variables.each { |v| assert_not_nil Person.instance_variable_get("@#{v}") }
+ Person.column :new_column, :string
+ cached_variables.each { |v| assert_nil Person.instance_variable_get("@#{v}") }
+ end
+end
View
6 vendor/plugins/active_record_without_table/test/database.yml
@@ -0,0 +1,6 @@
+mysql:
+ :adapter: mysql
+ :host: localhost
+ :username: rails
+ :password:
+ :database: rails_plugin_test

0 comments on commit b3f414c

Please sign in to comment.