addresslogic_rails 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/README.rdoc +73 -0
- data/Rakefile +2 -0
- data/addresslogic_rails.gemspec +21 -0
- data/lib/addresslogic_rails.rb +70 -0
- data/lib/addresslogic_rails/version.rb +3 -0
- metadata +75 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
= Address Logic
|
2
|
+
|
3
|
+
This is a simple library that takes away the annoyances of displaying addresses in a view. Since various parts of an address can sometimes be optional, such as "street2", displaying addresses was not fun:
|
4
|
+
|
5
|
+
== Example ERB use
|
6
|
+
|
7
|
+
<%= address.street1 %>
|
8
|
+
<br />
|
9
|
+
<% if !address.street2.blank? %>
|
10
|
+
<%= address.street2 %>
|
11
|
+
<% end %>
|
12
|
+
<br />
|
13
|
+
<%= "#{address.city}, #{address.state} #{address.zip}" %>
|
14
|
+
<br />
|
15
|
+
<%= address.country %>
|
16
|
+
|
17
|
+
That's just ugly. Instead, you can do this with Address Logic:
|
18
|
+
|
19
|
+
<%= address.address_parts.join("<br />") %>
|
20
|
+
|
21
|
+
== Example HAML use
|
22
|
+
|
23
|
+
# some HAML view
|
24
|
+
= address.street1
|
25
|
+
%br
|
26
|
+
- if !address.street2.blank?
|
27
|
+
= address.street2
|
28
|
+
%br
|
29
|
+
== #{address.city}, #{address.state} #{address.zip}
|
30
|
+
%br
|
31
|
+
= address.country
|
32
|
+
|
33
|
+
That's just ugly. Instead, you can do this with Address Logic:
|
34
|
+
|
35
|
+
= address.address_parts.join("<br />")
|
36
|
+
|
37
|
+
Or, what about a single line address?
|
38
|
+
|
39
|
+
= address.address_parts.join(", ")
|
40
|
+
|
41
|
+
Maybe you only want city and state:
|
42
|
+
|
43
|
+
= address.address_parts(:only => [:city, :state]).join(", ")
|
44
|
+
|
45
|
+
Granted the above is probably easier without using AddressLogic, but it will purge any blank items in the address, so if city or state are optional then it would be cleaner to use AddressLogic.
|
46
|
+
|
47
|
+
== Install and use
|
48
|
+
|
49
|
+
Install from rubyforge/gemcutter
|
50
|
+
|
51
|
+
$ sudo gem install addresslogic_rails
|
52
|
+
|
53
|
+
|
54
|
+
Then just include the AddressLogic module into any class of your choice. All that it assumes is that you have the street1, street2, city, state, zip, country (optional) methods.
|
55
|
+
|
56
|
+
class Address
|
57
|
+
apply_addresslogic
|
58
|
+
end
|
59
|
+
|
60
|
+
You can specify the fields too:
|
61
|
+
|
62
|
+
class Address
|
63
|
+
apply_addresslogic :fields => [:street1, :street2, :city, [:state, :zip], :country]
|
64
|
+
end
|
65
|
+
|
66
|
+
== Helpful links
|
67
|
+
|
68
|
+
* <b>Documentation:</b> (Coming soon)
|
69
|
+
|
70
|
+
|
71
|
+
== Authors / Copyright
|
72
|
+
* Copyright (c) 2009 Ben Johnson of [Binary Logic](http://www.binarylogic.com), released under the MIT license
|
73
|
+
* This release (c) 2011 Mike Simkins of [G7OBS Software](http://software.g7obs.com), released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "addresslogic_rails/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "addresslogic_rails"
|
7
|
+
s.version = AddresslogicRails::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Mike Simkins", "Ben Johnson"]
|
10
|
+
s.email = ["software@g7obs.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{AddressLogic for Rails 3}
|
13
|
+
s.description = %q{This is a gem port of binary logics 'addresslogic' to Rails 3 and a Gem}
|
14
|
+
|
15
|
+
s.rubyforge_project = "addresslogicr3"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module AddresslogicRails
|
2
|
+
def self.included(base)
|
3
|
+
base.extend ClassMethods
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
attr_accessor :addresslogic_options
|
8
|
+
|
9
|
+
# Mixes in useful methods for handling addresses.
|
10
|
+
#
|
11
|
+
# === Options
|
12
|
+
#
|
13
|
+
# * <tt>fields:</tt> array of fields (default: [:street1, :street2, [:city, [:state, :zip]], :country])
|
14
|
+
# * <tt>namespace:</tt> prefixes fields names with this, great for use with composed_of in ActiveRecord.
|
15
|
+
def apply_addresslogic(options = {})
|
16
|
+
n = options[:namespace]
|
17
|
+
options[:fields] ||= [
|
18
|
+
"#{n}street1".to_sym,
|
19
|
+
"#{n}street2".to_sym,
|
20
|
+
["#{n}city".to_sym, ["#{n}state".to_sym, "#{n}zip".to_sym]],
|
21
|
+
"#{n}country".to_sym
|
22
|
+
]
|
23
|
+
self.addresslogic_options = options
|
24
|
+
|
25
|
+
include AddresslogicRails::InstanceMethods
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module InstanceMethods
|
30
|
+
# Returns the parts of an address in an array. Example:
|
31
|
+
#
|
32
|
+
# ["Street1", "Street2", "City", "State Zip", "Country"]
|
33
|
+
#
|
34
|
+
# This makes displaying addresses on your view pretty simple:
|
35
|
+
#
|
36
|
+
# address.address_parts.join("<br />")
|
37
|
+
#
|
38
|
+
# === Options
|
39
|
+
#
|
40
|
+
# * <tt>only:</tt> fields you want included in the result
|
41
|
+
# * <tt>except:</tt> any fields you want excluded from the result
|
42
|
+
def address_parts(*args)
|
43
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
44
|
+
options[:only] = [options[:only]] if options[:only] && !options[:only].is_a?(Array)
|
45
|
+
options[:except] = [options[:except]] if options[:except] && !options[:except].is_a?(Array)
|
46
|
+
fields = args[0] || self.class.addresslogic_options[:fields]
|
47
|
+
level = args[1] || 0
|
48
|
+
|
49
|
+
parts = []
|
50
|
+
fields.each do |field|
|
51
|
+
if field.is_a?(Array)
|
52
|
+
has_sub_array = field.find { |item| item.is_a?(Array) }
|
53
|
+
separator = has_sub_array ? ", " : " "
|
54
|
+
sub_parts = address_parts(field, level + 1, options).join(separator)
|
55
|
+
next if sub_parts.empty?
|
56
|
+
parts << sub_parts
|
57
|
+
else
|
58
|
+
next if !respond_to?(field)
|
59
|
+
value = send(field)
|
60
|
+
next if value.to_s.strip == "" || (options[:only] && !options[:only].include?(field)) || (options[:except] && options[:except].include?(field))
|
61
|
+
parts << value
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
parts
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
Object.send(:include, AddresslogicRails)
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: addresslogic_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Mike Simkins
|
14
|
+
- Ben Johnson
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-03-22 00:00:00 +00:00
|
20
|
+
default_executable:
|
21
|
+
dependencies: []
|
22
|
+
|
23
|
+
description: This is a gem port of binary logics 'addresslogic' to Rails 3 and a Gem
|
24
|
+
email:
|
25
|
+
- software@g7obs.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- Gemfile
|
35
|
+
- README.rdoc
|
36
|
+
- Rakefile
|
37
|
+
- addresslogic_rails.gemspec
|
38
|
+
- lib/addresslogic_rails.rb
|
39
|
+
- lib/addresslogic_rails/version.rb
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: ""
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
hash: 3
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
hash: 3
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project: addresslogicr3
|
70
|
+
rubygems_version: 1.6.0
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: AddressLogic for Rails 3
|
74
|
+
test_files: []
|
75
|
+
|