has_location 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/has_location.rb +51 -0
  3. metadata +57 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 26aea366607dec70cab4b6894080a9cdbef72122
4
+ data.tar.gz: bcdfc129cdb2ce60739e8695e78d9fb2c567a874
5
+ SHA512:
6
+ metadata.gz: eb05e72930ef8d4cd8c216e624f6401881b1ca4e6233c7644a054e8ed0d098acdf8c14a6badd164436ef794e61a3471274556db701f07ff74273c4ece757f47f
7
+ data.tar.gz: 5474e05a163399d492c08580948e343bd99f83fdd0d34b2d05f0a496e5b2f44dd0338e2a43985b7b73e7990783d2f80adbe324689e1189b9baed3258929d112a
@@ -0,0 +1,51 @@
1
+ require 'active_record'
2
+
3
+ # class Company < ActiveRecord::Base
4
+ # has_location
5
+ # end
6
+
7
+ # The associated model must have the columns :location_id and :location_string.
8
+ #
9
+ require 'ruby-debug'
10
+ module HasLocation
11
+ def self.append_features(base)
12
+ super
13
+ base.extend ClassMethods
14
+ end
15
+
16
+ module ClassMethods
17
+ def has_location( options={} )
18
+ class_eval do
19
+
20
+ unless self.column_names.include?("location_id")
21
+ puts "WARNING: '#{self.name}' does not have a 'location_id' column. has_location will not work!"
22
+ end
23
+ unless self.column_names.include?("location_string")
24
+ puts "WARNING: '#{self.name}' does not have a 'location_string' column. has_location will not work!"
25
+ end
26
+ unless File.exists?("#{Rails.root}/app/models/location.rb") #Kernel.const_defined?("Location")
27
+ puts "WARNING: '#{Rails.root}/app/models/location.rb' does not exist. has_location will not work!"
28
+ end
29
+ unless Location.respond_to?(:normalize)
30
+ puts "WARNING: 'Location' does not respond to :normalize. has_location will not work!"
31
+ end
32
+
33
+ # attr_accessible :location_id, :location_string
34
+ belongs_to :location
35
+
36
+ before_save :detect_location
37
+
38
+ def detect_location
39
+ return unless self.location_string_changed?
40
+ self.location_id = (Location.normalize( self.location_string ).id rescue nil)
41
+ end
42
+ end
43
+ end
44
+
45
+ def percent_with_location
46
+ ((where("location_id IS NOT NULL").count.to_f / count.to_f) * 100).round(1)
47
+ end
48
+ end
49
+ end
50
+
51
+ ActiveRecord::Base.class_eval { include HasLocation }
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_location
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Jon Collier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 2.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 2.3.0
27
+ description: Easily associate locations with your models.
28
+ email: github@joncollier.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/has_location.rb
34
+ homepage: https://github.com/atlantistechnology/has_location
35
+ licenses: []
36
+ metadata: {}
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 2.0.3
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Easily associate locations with your models.
57
+ test_files: []