diff --git a/.document b/.document new file mode 100644 index 0000000..3d618dd --- /dev/null +++ b/.document @@ -0,0 +1,5 @@ +lib/**/*.rb +bin/* +- +features/**/*.feature +LICENSE.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2288186 --- /dev/null +++ b/.gitignore @@ -0,0 +1,42 @@ +# rcov generated +coverage + +# rdoc generated +rdoc + +# yard generated +doc +.yardoc + +# bundler +.bundle + +# jeweler generated +pkg + +# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore: +# +# * Create a file at ~/.gitignore +# * Include files you want ignored +# * Run: git config --global core.excludesfile ~/.gitignore +# +# After doing this, these files will be ignored in all your git projects, +# saving you from having to 'pollute' every project you touch with them +# +# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line) +# +# For MacOS: +# +#.DS_Store +# +# For TextMate +#*.tmproj +#tmtags +# +# For emacs: +#*~ +#\#* +#.\#* +# +# For vim: +#*.swp diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 0000000..630e38f --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,3 @@ +Tue, Mar 8, 2011 +- - - - - - - - - +- Initial Release diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..b6367d2 --- /dev/null +++ b/Gemfile @@ -0,0 +1,8 @@ +source 'http://rubygems.org' + +group :development do + gem 'rake' + gem 'jeweler' + gem 'rspec' + gem 'cucumber' +end diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..e650a12 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,38 @@ +GEM + remote: http://rubygems.org/ + specs: + builder (3.0.0) + cucumber (0.10.0) + builder (>= 2.1.2) + diff-lcs (~> 1.1.2) + gherkin (~> 2.3.2) + json (~> 1.4.6) + term-ansicolor (~> 1.0.5) + diff-lcs (1.1.2) + gherkin (2.3.3) + json (~> 1.4.6) + git (1.2.5) + jeweler (1.5.2) + bundler (~> 1.0.0) + git (>= 1.2.5) + rake + json (1.4.6) + rake (0.8.7) + rspec (2.4.0) + rspec-core (~> 2.4.0) + rspec-expectations (~> 2.4.0) + rspec-mocks (~> 2.4.0) + rspec-core (2.4.0) + rspec-expectations (2.4.0) + diff-lcs (~> 1.1.2) + rspec-mocks (2.4.0) + term-ansicolor (1.0.5) + +PLATFORMS + ruby + +DEPENDENCIES + cucumber + jeweler + rake + rspec diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..e92b829 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,20 @@ +Copyright (c) 2011 Paul Campbell + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..f554bf0 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# Address Engine # + +An `Address` model for your Rails 3 apps. Fields inspired from Amazon.com: + + create_table "addresses", :force => true do |t| + t.string "email" + t.string "firstname" + t.string "lastname" + t.text "address" + t.string "city" + t.string "state_province_region" + t.string "zip_postal_code" + t.string "country" + t.string "phone" + t.datetime "created_at" + t.datetime "updated_at" + t.string "middlename" + end + +## Copyright ## + +Copyright (c) 2011 Paul Campbell. See LICENSE.txt for +further details. + diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..508f6ec --- /dev/null +++ b/Rakefile @@ -0,0 +1,56 @@ +require 'rubygems' +require 'bundler' +begin + Bundler.setup(:default, :development) +rescue Bundler::BundlerError => e + $stderr.puts e.message + $stderr.puts "Run `bundle install` to install missing gems" + exit e.status_code +end +require 'rake' + +require 'jeweler' +Jeweler::Tasks.new do |gem| + # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options + gem.name = "address_engine" + gem.homepage = "http://github.com/paulca/address_engine" + gem.license = "MIT" + gem.summary = %Q{For apps that need an address table in a hurry!} + gem.description = %Q{Inspired by the address fields on amazon.com} + gem.email = "paul@rslw.com" + gem.authors = ["Paul Campbell"] + # Include your dependencies below. Runtime dependencies are required when using your gem, + # and development dependencies are only needed for development (ie running rake tasks, tests, etc) + gem.add_runtime_dependency 'rails', '~>3.0' + gem.add_runtime_dependency 'activerecord', '~>3.0' + gem.add_runtime_dependency 'carmen', '~>0.2.6' + gem.files = FileList["[A-Za-z]*", "lib/**/*", "app/**/*"] + # gem.add_development_dependency 'rspec', '> 1.2.3' +end +Jeweler::RubygemsDotOrgTasks.new + +require 'rspec/core' +require 'rspec/core/rake_task' +RSpec::Core::RakeTask.new(:spec) do |spec| + spec.pattern = FileList['spec/**/*_spec.rb'] +end + +RSpec::Core::RakeTask.new(:rcov) do |spec| + spec.pattern = 'spec/**/*_spec.rb' + spec.rcov = true +end + +require 'cucumber/rake/task' +Cucumber::Rake::Task.new(:features) + +task :default => :spec + +require 'rake/rdoctask' +Rake::RDocTask.new do |rdoc| + version = File.exist?('VERSION') ? File.read('VERSION') : "" + + rdoc.rdoc_dir = 'rdoc' + rdoc.title = "address_engine #{version}" + rdoc.rdoc_files.include('README*') + rdoc.rdoc_files.include('lib/**/*.rb') +end diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..3eefcb9 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +1.0.0 diff --git a/app/models/configurable.rb b/app/models/configurable.rb new file mode 100644 index 0000000..0a99398 --- /dev/null +++ b/app/models/configurable.rb @@ -0,0 +1,67 @@ +class Address < ActiveRecord::Base + + validates_presence_of :firstname, :lastname + validates_presence_of :country + validates_format_of :phone, :with => /^[0-9\-\+ ]*$/ + validates_format_of :email, :with => /^[^@]*@.*\.[^\.]*$/, :message => 'is invalid. Please enter an address in the format of your@email_address.com' + validates_presence_of :phone, :message => ' is required.' + + def country_name + Carmen::country_name(country) + end + + def order + order_as_shipping.present? ? order_as_shipping : order_as_billing + end + + def shipping_rules + ShippingRule.for_country(country) + end + + def filled_in? + address? && country? + end + + def name + [].tap do |out| + out << firstname if firstname.present? + out << lastname if lastname.present? + end.join(' ') + end + + def parts + [].tap do |out| + out << name + if address.present? + address.split("\n").each do |line| + out << line + end + end + out << city if city.present? + out << state_province_region if state_province_region.present? + out << zip_postal_code if zip_postal_code.present? + out << country_name if country.present? + end + end + + def readable_parts + [].tap do |out| + out << name + + if address.present? + address.split("\n").each do |line| + out << line + end + end + + last_line = [] + last_line << city if city.present? + last_line << state_province_region if state_province_region.present? + last_line << zip_postal_code if zip_postal_code.present? + + out << last_line.join(', ') + out << country_name if country.present? + end + end + +end \ No newline at end of file diff --git a/lib/address_engine.rb b/lib/address_engine.rb new file mode 100644 index 0000000..0d0a89f --- /dev/null +++ b/lib/address_engine.rb @@ -0,0 +1 @@ +require 'address_engine/engine' \ No newline at end of file diff --git a/lib/address_engine/engine.rb b/lib/address_engine/engine.rb new file mode 100644 index 0000000..a883a9e --- /dev/null +++ b/lib/address_engine/engine.rb @@ -0,0 +1,4 @@ +module AddressEngine + class Engine < Rails::Engine + end +end \ No newline at end of file diff --git a/lib/generators/address_engine/install_generator.rb b/lib/generators/address_engine/install_generator.rb new file mode 100644 index 0000000..9970d96 --- /dev/null +++ b/lib/generators/address_engine/install_generator.rb @@ -0,0 +1,23 @@ +require 'rails/generators' +module AddressEngine + class InstallGenerator < Rails::Generators::Base + include Rails::Generators::Migration + + + def self.source_root + @source_root ||= File.join(File.dirname(__FILE__), 'templates') + end + + def self.next_migration_number(dirname) + if ActiveRecord::Base.timestamped_migrations + Time.now.utc.strftime("%Y%m%d%H%M%S") + else + "%.3d" % (current_migration_number(dirname) + 1) + end + end + + def create_migration_file + migration_template 'migration.rb', 'db/migrate/create_addresses.rb' + end + end +end \ No newline at end of file diff --git a/lib/generators/address_engine/templates/configurable.yml b/lib/generators/address_engine/templates/configurable.yml new file mode 100644 index 0000000..48197ee --- /dev/null +++ b/lib/generators/address_engine/templates/configurable.yml @@ -0,0 +1,33 @@ +# This file controls what config variables you want to be able to allow your users +# to set, as well as those you'll be able to access from within the application. +# +# If you want to be able to access a string config[:site_title], for example: +# +# site_title: +# name: Site Title +# type: string +# default: My Site +# +# 'name' is the name that appears in the edit form +# +# 'type' can be 'string' for a text field, 'password' for a password field or 'text' for a text area +# 'type' defaults to 'string' +# +# 'default' is the default value to use if there's no entry in the database. Otherwise, nil will be returned +# +# Some Examples: +# +# site_title: +# name: Site Title +# default: My Site +# type: string +# +# site_description: +# name: Description for Google +# default: Lots of Awesomeness Here +# type: text +# +# secret: +# name: Secret Password for Accessing Secret Areas +# default: secret +# type: password \ No newline at end of file diff --git a/lib/generators/address_engine/templates/migration.rb b/lib/generators/address_engine/templates/migration.rb new file mode 100644 index 0000000..8c5910c --- /dev/null +++ b/lib/generators/address_engine/templates/migration.rb @@ -0,0 +1,22 @@ +class CreateAddresses < ActiveRecord::Migration + def self.up + create_table "addresses", :force => true do |t| + t.string "email" + t.string "firstname" + t.string "lastname" + t.text "address" + t.string "city" + t.string "state_province_region" + t.string "zip_postal_code" + t.string "country" + t.string "phone" + t.datetime "created_at" + t.datetime "updated_at" + t.string "middlename" + end + end + + def self.down + drop_table :addresses + end +end \ No newline at end of file