is_addressable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in digs.gemspec
4
+ gemspec
@@ -0,0 +1,23 @@
1
+ = is_addressable
2
+
3
+ Adds address attributes to a model.
4
+
5
+ == Installation
6
+
7
+ gem 'is_addressable'
8
+ bundle
9
+
10
+ == Generate Migrations
11
+
12
+ Addresses are stored in an addresses table. You can create this table by using the commands below.
13
+
14
+ rails g is_addressable:install
15
+ rake db:migrate
16
+
17
+ == Use in views
18
+
19
+ You can use this helper to render the address fields.
20
+
21
+ <%= form_for @model do |f| %>
22
+ <%= address f %>
23
+ <% end %>
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "is_addressable/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "is_addressable"
7
+ s.version = IsAddressable::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Phil McClure"]
10
+ s.email = ["pmcclure@rumblelabs.com"]
11
+ s.homepage = "http://rumblelabs.com"
12
+ s.summary = %q{Adds addressable attributes to a model}
13
+ s.description = %q{Adds addressable attributes to a model}
14
+
15
+ s.rubyforge_project = "is_addressable"
16
+ s.add_dependency('validates_as_uk_postcode')
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+ end
@@ -0,0 +1,2 @@
1
+ Description:
2
+ Generates (but does not run) a migration to add an addresses table.
@@ -0,0 +1,23 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module IsAddressable
5
+ class InstallGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+
8
+ source_root File.expand_path('../templates', __FILE__)
9
+ desc 'Generates (but does not run) a migration to add an addresses table.'
10
+
11
+ def self.next_migration_number(dirname) #:nodoc:
12
+ if ActiveRecord::Base.timestamped_migrations
13
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
14
+ else
15
+ "%.3d" % (current_migration_number(dirname) + 1)
16
+ end
17
+ end
18
+
19
+ def create_files
20
+ migration_template 'create_addresses.rb', 'db/migrate/create_addresses.rb'
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ class CreateAddresses < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :addresses do |t|
4
+ t.string :addressable_type, :null => false
5
+ t.integer :addressable_id, :null => false
6
+ t.string :street
7
+ t.string :town_or_city
8
+ t.string :country
9
+ t.string :additional
10
+ t.string :postcode
11
+ t.datetime :created_at
12
+ end
13
+ add_index :addresses, [:addressable_type, :addressable_id]
14
+ end
15
+
16
+ def self.down
17
+ remove_index :addresses, [:addressable_type, :addressable_id]
18
+ drop_table :addresses
19
+ end
20
+ end
@@ -0,0 +1,7 @@
1
+ require 'validates_as_uk_postcode'
2
+ require 'is_addressable/address'
3
+ require 'is_addressable/addressable'
4
+ require File.join(File.dirname(__FILE__), 'is_addressable/engine')
5
+
6
+ module IsAddressable
7
+ end
@@ -0,0 +1,14 @@
1
+ class Address < ActiveRecord::Base
2
+ belongs_to :addressable, :polymorphic => true
3
+ validates_length_of :street, :in => 1..100, :allow_nil => true, :allow_blank => true
4
+ validates_length_of :town_or_city, :in => 1..50, :allow_nil => true, :allow_blank => true
5
+ validates_length_of :additional, :maximum => 100, :allow_nil => true, :allow_blank => true
6
+
7
+ # Validate postcode if a uk address
8
+ validates_presence_of :postcode, :if => :should_validate_postcode?
9
+ validates :postcode, :postcode => true, :allow_nil => true, :allow_blank => true
10
+
11
+ def should_validate_postcode?
12
+ self.postcode.nil? && self.postcode.blank?
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ module IsAddressable
2
+ module Addressable
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def is_addressable
7
+ self.has_one :address, :as => :addressable, :dependent => :destroy
8
+ accepts_nested_attributes_for :address
9
+ end
10
+ end
11
+
12
+ def validate_address
13
+ unless self.address.valid?
14
+ self.address.errors.each do |key,message|
15
+ self.errors.add(key,message)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ ActiveRecord::Base.send :include, IsAddressable::Addressable
@@ -0,0 +1,3 @@
1
+ module IsAddressable
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: is_addressable
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Phil McClure
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-07 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: validates_as_uk_postcode
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: Adds addressable attributes to a model
36
+ email:
37
+ - pmcclure@rumblelabs.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - README.rdoc
48
+ - Rakefile
49
+ - is_addressable.gemspec
50
+ - lib/generators/is_addressable/USAGE
51
+ - lib/generators/is_addressable/install_generator.rb
52
+ - lib/generators/is_addressable/templates/create_addresses.rb
53
+ - lib/is_addressable.rb
54
+ - lib/is_addressable/address.rb
55
+ - lib/is_addressable/addressable.rb
56
+ - lib/is_addressable/version.rb
57
+ has_rdoc: true
58
+ homepage: http://rumblelabs.com
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options: []
63
+
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project: is_addressable
87
+ rubygems_version: 1.5.2
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Adds addressable attributes to a model
91
+ test_files: []
92
+