# frozen_string_literal: true # == Schema Information # # Table name: post_locations # # id :bigint not null, primary key # id_at_location :integer # location :string # created_at :datetime not null # updated_at :datetime not null # post_id :bigint # # Indexes # # index_post_locations_on_post_id (post_id) # class Post::Location < ApplicationRecord # map of valid location names to their URL prefixes #noinspection RubyStringKeysInHashInspection LOCATIONS = { 'derpibooru' => 'https://derpibooru.org/images/', 'ponybooru' => 'https://ponybooru.org/images/', 'rainbooru' => 'https://rainbooru.org/img/', 'ponerpics' => 'https://ponerpics.org/images/', 'foalcon' => 'https://booru.foalcon.com/post/', 'lyrabooru' => 'https://lyrabooru.org/post/view/', 'manebooru' => 'https://manebooru.art/images/' } # map of other sites' domain names to their location names INVERSE_LOCATIONS = { 'derpibooru.org' => 'derpibooru', 'trixiebooru.org' => 'derpibooru', 'ponybooru.org' => 'ponybooru', 'rainbooru.org' => 'rainbooru', 'ponerpics.org' => 'ponerpics', 'booru.foalcon.com' => 'foalcon', 'lyrabooru.org' => 'lyrabooru', 'manebooru.art' => 'manebooru' } belongs_to :post, inverse_of: :locations validates :location, inclusion: LOCATIONS.keys validates :id_at_location, numericality: true validates_uniqueness_of :location, scope: :post_id def url_at_location LOCATIONS[location] + id_at_location.to_s end def as_json(*) { location: location, id_at_location: id_at_location, url_at_location: url_at_location } end end