aboutsummaryrefslogtreecommitdiff
path: root/app/models/resource.rb
diff options
context:
space:
mode:
authorhellekin <hellekin@cepheide.org>2020-10-09 10:24:06 +0200
committerhellekin <hellekin@cepheide.org>2020-10-09 10:24:06 +0200
commitf48ce2f4c934fde3862cdad593eececc7a567d61 (patch)
treed2c874f8ebf4686d07052fa333032c04ef49e861 /app/models/resource.rb
parent31850c6ca118b7828dbaa3ad1a87dab4287718f5 (diff)
downloadincommon-map-f48ce2f4c934fde3862cdad593eececc7a567d61.tar.gz
Add Classifications and Resource validations
- Turn `has_and_belongs_to_many` into `has_many :through`: now, resources and sections are related through Classifications. - Refactor usage of jsonb column to use ActiveRecord validations - Attention! store_accessor: NOTE: If you are using structured database data types (eg. PostgreSQL hstore/json, or MySQL 5.7+ json) there is no need for the serialization provided by .store. Simply use .store_accessor instead to generate the accessor methods. Be aware that these columns use a string keyed hash and do not allow access using a symbol. NOTE: The default validations with the exception of uniqueness will work. For example, if you want to check for uniqueness with hstore you will need to use a custom validation to handle it. https://api.rubyonrails.org/classes/ActiveRecord/Store.html
Diffstat (limited to 'app/models/resource.rb')
-rw-r--r--app/models/resource.rb68
1 files changed, 49 insertions, 19 deletions
diff --git a/app/models/resource.rb b/app/models/resource.rb
index cd43bf9..906933d 100644
--- a/app/models/resource.rb
+++ b/app/models/resource.rb
@@ -1,26 +1,56 @@
+require_dependency 'phony_rails'
+
class Resource < ApplicationRecord
# Universally Unique Identifier :uuid
include UUIDParameter
belongs_to :agent
- has_and_belongs_to_many :sections
-
- # Figure out the requested property name
- def method_missing(name, *args, &block)
- Rails.logger.info("method_missing: #{name} // #{feature['properties'][name.to_s]}")
- if feature['properties'].key?(name.to_s)
- feature['properties'][name.to_s]
- else
- case name.to_s
- when 'lon', 'longitude'
- feature['geometry']['coordinates'].first
- when 'lat', 'latitude'
- feature['geometry']['coordinates'].last
- when 'geo_type'
- feature['geometry']['type']
- else
- super
- end
- end
+ has_many :classifications
+ has_many :sections, through: :classifications
+
+ serialize :feature, HashSerializer
+ store_accessor :feature, :name, :summary, :description, :email, :source, :address, :postal_code, :city, :phone_number, :website
+
+ validates_associated :agent
+
+ validates :name,
+ presence: true,
+ length: { in: 3..64 }
+
+ validates :email,
+ with: { format: URI::MailTo::EMAIL_REGEXP },
+ allow_blank: true
+
+ validates :source,
+ inclusion: { in: Agent.pluck(:name) }
+
+ # TODO: Address,Postal Code,City validation
+
+ phony_normalize :phone_number, default_country_code: 'BE', normalize_when_valid: true
+ validates :phone_number,
+ phony_plausible: { ignore_record_country_code: true, ignore_record_country_number: true }
+
+ # Depends on validate_url Gem
+ validates :website,
+ url: { allow_blank: true }
+
+ # Accessors for feature['geometry']
+ def geo_type
+ self.feature['geometry']['type']
+ end
+
+ # You can use, e.g.: res.longitude = 0.123
+ def longitude
+ feature['geometry']['coordinates'][0]
+ end
+ def longitude=(value)
+ feature['geometry']['coordinates'][0] = value
+ end
+ # You can use, e.g.: res.latitude = 0.123
+ def latitude
+ feature['geometry']['coordinates'][1]
+ end
+ def latitude=(value)
+ feature['geometry']['coordinates'][1] = value
end
end