Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: generate resource from model attributes #1244

Conversation

Paul-Bob
Copy link
Contributor

@Paul-Bob Paul-Bob commented Sep 18, 2022

Description

Fixes #182

Todo:

  • Hook into rails generate model command and run the resource generator (see how factory_bot does it)

Usage:

New model:

$ rails g model car make kms:integer

      invoke  active_record
      create    db/migrate/20221007210116_create_cars.rb
      create    app/models/car.rb
      invoke    test_unit
      create      test/models/car_test.rb
      invoke      factory_bot
      create        test/factories/cars.rb
      invoke  avo_resource
Avo pro 2.16.0
      create    app/avo/resources/car_resource.rb
      create    app/controllers/avo/cars_controller.rb

Will generate the normal scaffolding, the avo controller and will auto-fill the resource:

class CarResource < Avo::BaseResource
  self.title = :id
  self.includes = []
  # self.search_query = -> do
  #   scope.ransack(id_eq: params[:q], m: "or").result(distinct: false)
  # end

  field :id, as: :id
  # Generated fields from model
  field :make, as: :text
  field :kms, as: :number
  # add fields here
end

Extra option:

--without_resource will not generate the avo controller + resource.

$ rails g model car make kms:integer --without_resource

      invoke  active_record
      create    db/migrate/20221007210544_create_cars.rb
      create    app/models/car.rb
      invoke    test_unit
      create      test/models/car_test.rb
      invoke      factory_bot
      create        test/factories/cars.rb

Existent model:

If we have this model post:

# == Schema Information
#
# Table name: posts
#
#  id           :bigint           not null, primary key
#  name         :string
#  body         :text
#  is_featured  :boolean
#  published_at :datetime
#  user_id      :bigint
#  created_at   :datetime         not null
#  updated_at   :datetime         not null
#  status       :integer          default("draft")
#
class Post < ApplicationRecord
 enum status: [:draft, :published, :archived]

 validates :name, presence: true

 has_one_attached :cover_photo
 has_one_attached :audio
 has_many_attached :attachments

 belongs_to :user, optional: true
 has_many :comments, as: :commentable
 has_many :reviews, as: :reviewable

 acts_as_taggable_on :tags
end

We can generate the avo resource by running:
$ rails g avo:resource post

class PostResource < Avo::BaseResource
  self.title = :id
  self.includes = []
  # self.search_query = -> do
  #   scope.ransack(id_eq: params[:q], m: "or").result(distinct: false)
  # end

  field :id, as: :id
  # Generated fields from model
  field :name, as: :text
  field :body, as: :textarea
  field :is_featured, as: :boolean
  field :published_at, as: :datetime
  field :user_id, as: :number
  field :status, as: :select, enum: ::Post.statuses
  field :cover_photo, as: :file
  field :audio, as: :file
  field :attachments, as: :files
  field :user, as: :belongs_to
  field :comments, as: :has_many
  field :reviews, as: :has_many
  field :tags, as: :tags
  # add fields here
end

Extra option:

--model_class

Lets say we want to generate an mini-post resource and this resource will have the post model as model_class, we can do it by running:

$ rails g avo:resource mini-post --model_class post

class MiniPostResource < Avo::BaseResource
  self.title = :id
  self.includes = []
  self.model_class = ::Post
  # self.search_query = -> do
  #   scope.ransack(id_eq: params[:q], m: "or").result(distinct: false)
  # end

  field :id, as: :id
  # Generated fields from model
  field :name, as: :text
  field :body, as: :textarea
  field :is_featured, as: :boolean
  field :published_at, as: :datetime
  field :user_id, as: :number
  field :status, as: :select, enum: ::Post.statuses
  field :cover_photo, as: :file
  field :audio, as: :file
  field :attachments, as: :files
  field :user, as: :belongs_to
  field :comments, as: :has_many
  field :reviews, as: :has_many
  field :tags, as: :tags
  # add fields here
end

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works

@Paul-Bob Paul-Bob self-assigned this Sep 18, 2022
@codeclimate
Copy link

codeclimate bot commented Sep 18, 2022

Code Climate has analyzed commit 91cf1c5 and detected 4 issues on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 4

View more on Code Climate.

@codecov
Copy link

codecov bot commented Sep 21, 2022

Codecov Report

Base: 94.29% // Head: 93.86% // Decreases project coverage by -0.42% ⚠️

Coverage data is based on head (96425f2) compared to base (06d1f45).
Patch has no changes to coverable lines.

❗ Current head 96425f2 differs from pull request most recent head 91cf1c5. Consider uploading reports for the commit 91cf1c5 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1244      +/-   ##
==========================================
- Coverage   94.29%   93.86%   -0.43%     
==========================================
  Files         543      554      +11     
  Lines       10778    11361     +583     
==========================================
+ Hits        10163    10664     +501     
- Misses        615      697      +82     
Impacted Files Coverage Δ
lib/generators/avo/resource_generator.rb 45.37% <0.00%> (-54.63%) ⬇️
...ec/dummy/app/controllers/avo/courses_controller.rb 78.94% <0.00%> (-21.06%) ⬇️
app/components/avo/filters_component.rb 92.30% <0.00%> (-7.70%) ⬇️
app/helpers/avo/application_helper.rb 64.28% <0.00%> (-6.73%) ⬇️
.../dummy/app/avo/resources/photo_comment_resource.rb 95.00% <0.00%> (-5.00%) ⬇️
app/components/avo/index/grid_item_component.rb 46.66% <0.00%> (-3.34%) ⬇️
app/controllers/avo/search_controller.rb 92.95% <0.00%> (-3.20%) ⬇️
.../avo/fields/common/single_file_viewer_component.rb 39.28% <0.00%> (-1.63%) ⬇️
app/controllers/avo/base_controller.rb 92.18% <0.00%> (-1.22%) ⬇️
lib/avo/licensing/h_q.rb 82.65% <0.00%> (-1.05%) ⬇️
... and 54 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

lib/generators/avo/resource_generator.rb Outdated Show resolved Hide resolved
lib/generators/avo/resource_generator.rb Outdated Show resolved Hide resolved
lib/generators/avo/resource_generator.rb Show resolved Hide resolved
lib/generators/avo/resource_generator.rb Show resolved Hide resolved
@Paul-Bob Paul-Bob marked this pull request as ready for review October 9, 2022 10:32
@github-actions
Copy link
Contributor

This PR has been marked as stale because there was no activity for the past 15 days.

@github-actions github-actions bot added the Stale label Oct 25, 2022
@adrianthedev adrianthedev self-requested a review October 26, 2022 20:17
@adrianthedev
Copy link
Collaborator

Can we add some tests to this too? They can be modeled after our current ones.

@github-actions github-actions bot removed the Stale label Nov 1, 2022
@adrianthedev adrianthedev merged commit 2fa0c2a into avo-hq:main Nov 3, 2022
@adrianthedev adrianthedev mentioned this pull request Nov 3, 2022
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants