has_vcards 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,14 @@
1
+ has_vcards
2
+ ==========
3
+
4
+ Rails plugin providing VCard like contact and address models and helpers.
5
+
6
+
7
+ License
8
+ =======
9
+
10
+ Copyright (c) 2007-2010 Simon Hürlimann <simon.huerlimann@cyt.ch>
11
+ Copyright (c) 2008 Agrabah <http://www.agrabah.ch>
12
+ Copyright (c) 2007-2010 ZytoLabor <http://www.zyto-labor.com>
13
+
14
+ Released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ # coding: utf-8
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+
6
+ begin
7
+ GEM = "has_vcards"
8
+ AUTHOR = "Simon Hürlimann"
9
+ EMAIL = "simon.huerlimann@cyt.ch"
10
+ SUMMARY = "Rails gem providing VCard like contact and address models and helpers."
11
+ HOMEPAGE = "http://github.com/huerlisi/has_vcards/tree/master"
12
+
13
+ gem 'jeweler', '>= 1.0.0'
14
+ require 'jeweler'
15
+
16
+ Jeweler::Tasks.new do |s|
17
+ s.name = GEM
18
+ s.summary = SUMMARY
19
+ s.email = EMAIL
20
+ s.homepage = HOMEPAGE
21
+ s.description = SUMMARY
22
+ s.author = AUTHOR
23
+
24
+ s.require_path = 'lib'
25
+ s.files = %w(MIT-LICENSE README.markdown Rakefile) + Dir.glob("{lib,app,test,config}/**/*")
26
+
27
+ # Runtime dependencies: When installing has_vcards these will be checked if they are installed.
28
+ # Will be offered to install these if they are not already installed.
29
+ s.add_dependency 'activerecord'
30
+ s.add_dependency 'i18n'
31
+ end
32
+
33
+ Jeweler::GemcutterTasks.new
34
+ rescue LoadError
35
+ puts "[has_vcards:] Jeweler - or one of its dependencies - is not available. Install it with: sudo gem install jeweler -s http://gemcutter.org"
36
+ end
37
+
38
+ desc 'Default: run unit tests.'
39
+ task :default => :test
40
+
41
+ desc 'Test the has_vcards plugin.'
42
+ Rake::TestTask.new(:test) do |t|
43
+ t.libs << 'lib'
44
+ t.libs << 'test'
45
+ t.pattern = 'test/**/*_test.rb'
46
+ t.verbose = true
47
+ end
48
+
49
+ desc 'Generate documentation for the has_vcards plugin.'
50
+ Rake::RDocTask.new(:rdoc) do |rdoc|
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = 'has_vcards'
53
+ rdoc.options << '--line-numbers' << '--inline-source'
54
+ rdoc.rdoc_files.include('README.markdown')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
@@ -0,0 +1,13 @@
1
+ module HasVcardsHelper
2
+ def address(vcard, line_separator = '<br/>')
3
+ vcard.address_lines.map{|line| h(line)}.join(line_separator).html_safe
4
+ end
5
+
6
+ def full_address(vcard, line_separator = '<br/>')
7
+ vcard.full_address_lines.map{|line| h(line)}.join(line_separator).html_safe
8
+ end
9
+
10
+ def contact(vcard, line_separator = '<br/>', label_separator = ' ')
11
+ vcard.contact_lines(label_separator).map{|line| h(line)}.join(line_separator).html_safe
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ class Address < ActiveRecord::Base
2
+ belongs_to :vcard
3
+ end
@@ -0,0 +1,25 @@
1
+ class PhoneNumber < ActiveRecord::Base
2
+ belongs_to :vcard
3
+ belongs_to :object, :polymorphic => true
4
+
5
+ validates_presence_of :number
6
+
7
+ def label
8
+ case phone_number_type
9
+ when 'phone'
10
+ "Tel:"
11
+ when 'fax'
12
+ "Fax:"
13
+ when 'mobile'
14
+ "Mob:"
15
+ when 'email'
16
+ "Mail:"
17
+ else
18
+ ""
19
+ end
20
+ end
21
+
22
+ def to_s(separator = " ")
23
+ return [label, number].compact.join(separator)
24
+ end
25
+ end
@@ -0,0 +1,131 @@
1
+ class Vcard < ActiveRecord::Base
2
+ has_one :address, :autosave => true, :validate => true
3
+ accepts_nested_attributes_for :address
4
+ delegate :post_office_box, :extended_address, :street_address, :locality, :region, :postal_code, :country_name, :to => :address
5
+ delegate :post_office_box=, :extended_address=, :street_address=, :locality=, :region=, :postal_code=, :country_name=, :to => :address
6
+
7
+ has_many :addresses, :autosave => true, :validate => true
8
+ accepts_nested_attributes_for :addresses
9
+
10
+ scope :active, :conditions => {:active => true}
11
+ scope :by_name, lambda {|name| {:conditions => self.by_name_conditions(name)}}
12
+
13
+ belongs_to :object, :polymorphic => true
14
+
15
+ # Convenience accessors
16
+ def full_name
17
+ result = read_attribute(:full_name)
18
+ result ||= [ family_name, given_name ].compact.join(' ')
19
+
20
+ return result
21
+ end
22
+
23
+ def abbreviated_name
24
+ "#{given_name[0..0]}. #{family_name}"
25
+ end
26
+
27
+ # Advanced finders
28
+ def self.by_name_conditions(name)
29
+ ['vcards.full_name LIKE :name OR vcards.family_name LIKE :name OR vcards.given_name LIKE :name OR vcards.nickname LIKE :name', {:name => name}]
30
+ end
31
+
32
+ def self.find_by_name(name)
33
+ self.find(:first, :conditions => self.by_name_conditions(name))
34
+ end
35
+
36
+ def self.find_all_by_name(name)
37
+ self.find(:all, :conditions => self.by_name_conditions(name))
38
+ end
39
+
40
+ # Helper methods
41
+ def address_lines
42
+ lines = [street_address, "#{postal_code} #{locality}"]
43
+
44
+ # Only return non-empty lines
45
+ lines.map {|line| line.strip unless (line.nil? or line.strip.empty?)}.compact
46
+ end
47
+
48
+ def full_address_lines
49
+ lines = [honorific_prefix, full_name] + address_lines
50
+
51
+ # Only return non-empty lines
52
+ lines.map {|line| line.strip unless (line.nil? or line.strip.empty?)}.compact
53
+ end
54
+
55
+ def contact_lines(separator = " ")
56
+ lines = contacts.map{|p| p.to_s unless (p.number.nil? or p.number.strip.empty?)}
57
+
58
+ # Only return non-empty lines
59
+ lines.map {|line| line.strip unless (line.nil? or line.strip.empty?)}.compact
60
+ end
61
+
62
+ # Phone numbers
63
+ has_many :contacts, :class_name => 'PhoneNumber', :as => :object
64
+
65
+ has_many :phone_numbers, :class_name => 'PhoneNumber', :as => :object, :conditions => ["phone_number_type = ?", 'phone'], :after_add => :add_phone_number
66
+ def add_phone_number(number)
67
+ number.phone_number_type = 'phone'
68
+ number.save
69
+ end
70
+
71
+ def phone_number
72
+ phone_numbers.first
73
+ end
74
+
75
+ def phone_number=(number)
76
+ phone_numbers.build(:number => number, :phone_number_type => 'phone')
77
+ end
78
+
79
+ has_many :mobile_numbers, :class_name => 'PhoneNumber', :as => :object, :conditions => ["phone_number_type = ?", 'mobile'], :after_add => :add_mobile_number
80
+ def add_mobile_number(number)
81
+ number.phone_number_type = 'mobile'
82
+ number.save
83
+ end
84
+
85
+ def mobile_number
86
+ mobile_numbers.first
87
+ end
88
+
89
+ def mobile_number=(number)
90
+ mobile_numbers.build(:number => number, :phone_number_type => 'mobile')
91
+ end
92
+
93
+ has_many :fax_numbers, :class_name => 'PhoneNumber', :as => :object, :conditions => ["phone_number_type = ?", 'fax'], :after_add => :add_fax_number
94
+ def add_fax_number(number)
95
+ number.phone_number_type = 'fax'
96
+ number.save
97
+ end
98
+
99
+ def fax_number
100
+ fax_numbers.first
101
+ end
102
+
103
+ def fax_number=(number)
104
+ fax_numbers.build(:number => number, :phone_number_type => 'fax')
105
+ end
106
+
107
+ # Salutation
108
+ def salutation
109
+ case honorific_prefix
110
+ when 'Herr Dr. med.'
111
+ result = "Sehr geehrter Herr Dr. " + family_name
112
+ when 'Frau Dr. med.'
113
+ result = "Sehr geehrte Frau Dr. " + family_name
114
+ when 'Herr Dr.'
115
+ result = "Sehr geehrter Herr Dr. " + family_name
116
+ when 'Frau Dr.'
117
+ result = "Sehr geehrte Frau Dr. " + family_name
118
+ when 'Herr'
119
+ result = "Sehr geehrter Herr " + family_name
120
+ when 'Frau'
121
+ result = "Sehr geehrte Frau " + family_name
122
+ when 'Br.'
123
+ result = "Sehr geehrter Bruder " + family_name
124
+ when 'Sr.'
125
+ result = "Sehr geehrte Schwester " + family_name
126
+ else
127
+ result = "Sehr geehrte Damen und Herren"
128
+ end
129
+ return result
130
+ end
131
+ end
@@ -0,0 +1,12 @@
1
+ require 'has_vcards'
2
+ require 'rails'
3
+
4
+ module HasVcards
5
+ class Railtie < Rails::Engine
6
+ initializer :after_initialize do
7
+ ActionController::Base.helper HasVcardsHelper
8
+
9
+ ActiveRecord::Base.extend(HasVcardsClassMethods)
10
+ end
11
+ end
12
+ end
data/lib/has_vcards.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'has_vcards/railtie' if defined?(::Rails::Railtie)
2
+
3
+ module HasVcardsClassMethods
4
+ def has_vcards(options = {})
5
+ class_eval <<-end_eval
6
+ scope :by_name, lambda {|name| {:include => :vcard, :order => 'vcards.full_name', :conditions => Vcard.by_name_conditions(name)}}
7
+
8
+ has_one :vcard, :as => 'object', :autosave => true, :validate => true
9
+ delegate :full_name, :nickname, :family_name, :given_name, :additional_name, :honorific_prefix, :honorific_suffix, :to => :vcard
10
+ delegate :full_name=, :nickname=, :family_name=, :given_name=, :additional_name=, :honorific_prefix=, :honorific_suffix=, :to => :vcard
11
+
12
+ has_many :vcards, :as => 'object', :autosave => true, :validate => true
13
+ end_eval
14
+ end
15
+ end
@@ -0,0 +1,8 @@
1
+ require 'test/unit'
2
+
3
+ class VcardsTest < Test::Unit::TestCase
4
+ # Replace this with your real tests.
5
+ def test_this_plugin
6
+ flunk
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_vcards
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 5
8
+ - 1
9
+ version: 0.5.1
10
+ platform: ruby
11
+ authors:
12
+ - "Simon H\xC3\xBCrlimann"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-10 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activerecord
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: i18n
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :runtime
43
+ version_requirements: *id002
44
+ description: Rails gem providing VCard like contact and address models and helpers.
45
+ email: simon.huerlimann@cyt.ch
46
+ executables: []
47
+
48
+ extensions: []
49
+
50
+ extra_rdoc_files:
51
+ - README.markdown
52
+ files:
53
+ - MIT-LICENSE
54
+ - README.markdown
55
+ - Rakefile
56
+ - app/helpers/has_vcards_helper.rb
57
+ - app/models/address.rb
58
+ - app/models/phone_number.rb
59
+ - app/models/vcard.rb
60
+ - lib/has_vcards.rb
61
+ - lib/has_vcards/railtie.rb
62
+ - test/vcards_test.rb
63
+ has_rdoc: true
64
+ homepage: http://github.com/huerlisi/has_vcards/tree/master
65
+ licenses: []
66
+
67
+ post_install_message:
68
+ rdoc_options:
69
+ - --charset=UTF-8
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ requirements: []
87
+
88
+ rubyforge_project:
89
+ rubygems_version: 1.3.6
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Rails gem providing VCard like contact and address models and helpers.
93
+ test_files:
94
+ - test/vcards_test.rb