Sha256: 2ca89539080abf2924076059cef54a15b93e3514599506da5c967d6ce428f607

Contents?: true

Size: 829 Bytes

Versions: 15

Compression:

Stored size: 829 Bytes

Contents

class CategoryResource < Avo::BaseResource
  # tutorial (related category): https://youtu.be/WgNK-oINFww?t=328

  self.title = :title
  self.includes = []

  self.search_query = ->(params:) do
    scope
      .ransack(id_eq: params[:q], name_cont: params[:q], title_cont: params[:q], description_cont: params[:q], m: "or")
      .result(distinct: false)
  end

  field :id, as: :id
  field :title, as: :text
  field :excerpt, as: :text, only_on: :index
  field :description, as: :textarea

  # tutorial (computed field): https://youtu.be/WgNK-oINFww?t=514
  field :posts_count, as: :text do |model|
    model.posts.count
  end

  # tutorial (attach scope): https://youtu.be/WgNK-oINFww?t=423
  field :posts, as: :has_many, attach_scope: -> { 
    query.where.not(category_id: parent.id).or(query.where(category_id: nil))
  }
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
rails_app_generator-0.3.6 after_templates/addons/avo/app/avo/resources/category_resource.rb
rails_app_generator-0.3.5 after_templates/addons/avo/app/avo/resources/category_resource.rb
rails_app_generator-0.3.4 after_templates/addons/avo/app/avo/resources/category_resource.rb
rails_app_generator-0.3.3 after_templates/addons/avo/app/avo/resources/category_resource.rb
rails_app_generator-0.3.2 after_templates/addons/avo/app/avo/resources/category_resource.rb
rails_app_generator-0.3.1 after_templates/addons/avo/app/avo/resources/category_resource.rb
rails_app_generator-0.3.0 after_templates/addons/avo/app/avo/resources/category_resource.rb
rails_app_generator-0.2.43 after_templates/addons/avo/app/avo/resources/category_resource.rb
rails_app_generator-0.2.42 after_templates/addons/avo/app/avo/resources/category_resource.rb
rails_app_generator-0.2.41 after_templates/addons/avo/app/avo/resources/category_resource.rb
rails_app_generator-0.2.40 after_templates/addons/avo/app/avo/resources/category_resource.rb
rails_app_generator-0.2.39 after_templates/addons/avo/app/avo/resources/category_resource.rb
rails_app_generator-0.2.38 after_templates/addons/avo/app/avo/resources/category_resource.rb
rails_app_generator-0.2.37 after_templates/addons/avo/app/avo/resources/category_resource.rb
rails_app_generator-0.2.36 after_templates/addons/avo/app/avo/resources/category_resource.rb