Sha256: 5d1fdbaa97612be349eab7e3cbb3a92285772fc83f6d64f1bc2513d311e5058e

Contents?: true

Size: 1.99 KB

Versions: 6

Compression:

Stored size: 1.99 KB

Contents

class Recipe < Spyke::Base
  has_many :groups
  has_one :image
  has_one :background_image, class_name: 'Image', uri: nil
  has_one :alternate, class_name: 'Recipe', uri: '/recipes/:recipe_id/alternates/recipe'
  belongs_to :user

  scope :published, -> { where(status: 'published') }
  scope :approved, -> { where(approved: true) }
  attributes :title

  before_save :before_save_callback
  before_create :before_create_callback
  before_update :before_update_callback

  accepts_nested_attributes_for :image, :user, :groups

  def self.page(number = nil)
    result = all
    result = result.where(page: number) if number
    result
  end

  def ingredients
    groups.flat_map(&:ingredients)
  end

  private

    def before_create_callback; end
    def before_update_callback; end
    def before_save_callback; end
end

class Image < Spyke::Base
  method_for :create, :put
end

class StepImage < Image
end

class RecipeImage < Image
  uri '/recipes/:recipe_id/image'
  validates :url, presence: true
  attributes :url
  include_root_in_json false
end

class Group < Spyke::Base
  has_many :ingredients, uri: nil
  accepts_nested_attributes_for :ingredients

  def self.build_default
    group_1 = build(name: 'Condiments')
    group_1.ingredients.build(name: 'Salt')
    group_2 = build(name: 'Tools')
    group_2.ingredients.build(name: 'Spoon')
  end
end

class Ingredient < Spyke::Base
  uri '/recipes/:recipe_id/ingredients/(:id)'
end

class User < Spyke::Base
  has_many :recipes
end

class Photo < Spyke::Base
  uri '/images/photos/(:id)'
end

class Comment < Spyke::Base
  scope :approved, -> { where(comment_approved: true) }
end

class Search
  def initialize(query)
    @query = query
  end

  def recipes
    @recipes ||= Recipe.where(query: @query)
  end

  def suggestions
    recipes.metadata[:suggestions]
  end
end

module Cookbook
  class Tip < Spyke::Base
    uri '/tips/(:id)'
    has_many :likes, class_name: 'Cookbook::Like', uri: '/tips/:cookbook_tip_id/likes/(:id)'
  end

  class Like < Spyke::Base
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
spyke-1.8.2 test/support/fixtures.rb
spyke-1.8.1 test/support/fixtures.rb
spyke-1.8.0 test/support/fixtures.rb
spyke-1.7.2 test/support/fixtures.rb
spyke-1.7.1 test/support/fixtures.rb
spyke-1.7.0 test/support/fixtures.rb