mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-10-31 03:09:06 -04:00
Declare read-only resources and the owner relationship once each, make the resource hook helpers private, simplify token auth, and cache the generated API docs for an hour.
28 lines
860 B
Ruby
28 lines
860 B
Ruby
# frozen_string_literal: true
|
|
|
|
class CropResource < ApplicationResource
|
|
self.model = Crop
|
|
read_only
|
|
secondary_endpoint '/crops/search', [:search]
|
|
|
|
attribute :name, :string
|
|
attribute :en_wikipedia_url, :string
|
|
attribute :perennial, :boolean
|
|
attribute :median_lifespan, :integer
|
|
attribute :median_days_to_first_harvest, :integer
|
|
attribute :median_days_to_last_harvest, :integer
|
|
filter_only :approval_status, :string
|
|
|
|
default_filter :approval_status do |scope|
|
|
scope.where(approval_status: 'approved')
|
|
end
|
|
|
|
belongs_to :parent, resource: CropResource
|
|
filter_only :parent_id, :integer
|
|
has_many :plantings
|
|
has_many :seeds
|
|
has_many :harvests
|
|
has_many :photos, foreign_key: :crop_id,
|
|
base_scope: -> { Photo.joins(:photo_associations).select('photos.*, photo_associations.crop_id AS crop_id').distinct }
|
|
end
|