Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

bug with inclusion of polymorphic association

Đang mở
#1,105 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
25/100
Loại issue
Lỗi
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
rails, ruby, sqlite
Lĩnh vực
api, backend, database

Hướng nghiên cứu

Chạy bản tái hiện Minitest được nhúng và so sánh test_patch_linkage_options_with_android với test_fetch_option_linked_with_android. Theo dõi quan hệ đa hình optionable của OptionResource qua controller và route JSONAPI. Được xem là hoàn tất khi cả hai request đều trả về thành công và association optionable có thể được liên kết và đưa vào như các test dự định.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Priority: High Target: 0.10 Type: Bug

reproduced with gist

begin
  require 'bundler/inline'
  require 'bundler'
rescue LoadError => e
  STDERR.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
  raise e
end

gemfile(true, ui: ENV['SILENT'] ? Bundler::UI::Silent.new : Bundler::UI::Shell.new) do
  source 'https://rubygems.org'

  gem 'rails', require: false
  gem 'sqlite3', platform: :mri

  gem 'activerecord-jdbcsqlite3-adapter',
      git: 'https://github.com/jruby/activerecord-jdbc-adapter',
      branch: 'rails-5',
      platform: :jruby

  if ENV['JSONAPI_RESOURCES_PATH']
    gem 'jsonapi-resources', path: ENV['JSONAPI_RESOURCES_PATH'], require: false
  else
    gem 'jsonapi-resources', git: 'https://github.com/cerebris/jsonapi-resources', require: false
  end

end

# prepare active_record database
require 'active_record'

class NullLogger < Logger
  def initialize(*_args)
  end

  def add(*_args, &_block)
  end
end

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = ENV['SILENT'] ? NullLogger.new : Logger.new(STDOUT)
ActiveRecord::Migration.verbose = !ENV['SILENT']

ActiveRecord::Schema.define do
  # Add your schema here
  create_table :options, force: true do |t|
    t.integer :optionable_id
    t.string :optionable_type
    t.boolean :enabled, default: false, null: false
  end

  create_table :androids, force: true do |t|
    t.string :version_name
  end
end

# create models
class Option < ActiveRecord::Base
  belongs_to :optionable, polymorphic: true, required: false
end

class Android < ActiveRecord::Base
  has_one :option, as: :optionable
end

# prepare rails app
require 'action_controller/railtie'
# require 'action_view/railtie'
require 'jsonapi-resources'

class ApplicationController < ActionController::Base
end

# prepare jsonapi resources and controllers
class OptionsController < ApplicationController
  include JSONAPI::ActsAsResourceController
end

class OptionResource < JSONAPI::Resource
  attribute :enabled
  has_one :optionable, polymorphic: true, class_name: 'Android'
end

class AndroidResource < JSONAPI::Resource
  attribute :version_name
end

class TestApp < Rails::Application
  config.root = File.dirname(__FILE__)
  config.logger = ENV['SILENT'] ? NullLogger.new : Logger.new(STDOUT)
  Rails.logger = config.logger

  secrets.secret_token = 'secret_token'
  secrets.secret_key_base = 'secret_key_base'

  config.eager_load = false
end

# initialize app
Rails.application.initialize!

JSONAPI.configure do |config|
  config.json_key_format = :underscored_key
  config.route_format = :underscored_key
end

# draw routes
Rails.application.routes.draw do
  jsonapi_resources :options, only: [:show, :update]
end

# prepare tests
require 'minitest/autorun'
require 'rack/test'

# Replace this with the code necessary to make your test fail.
class BugTest < Minitest::Test
  include Rack::Test::Methods

  def json_api_headers
    {'Accept' => JSONAPI::MEDIA_TYPE, 'CONTENT_TYPE' => JSONAPI::MEDIA_TYPE}
  end

  def test_patch_linkage_options_with_android
    android = Android.create! version_name: '1.0'
    option = Option.create!
    json_request = {
        data: {
            id: option.id.to_s,
            type: 'options',
            relationships: {
                optionable: {
                    data: {
                        id: android.id.to_s,
                        type: 'androids'
                    }
                }
            }
        }
    }
    patch "/options/#{option.id}?include=optionable", json_request.to_json, json_api_headers
    assert last_response.ok?
    assert android, option.reload.optionable
  end

  def test_fetch_option_linked_with_android
    android = Android.create! version_name: '1.0'
    option = Option.create! optionable: android
    get "/options/#{option.id}?include=optionable", nil, json_api_headers
    assert last_response.ok?
  end

  private

  def app
    Rails.application
  end
end
Ngôn ngữ chính
Ruby
Star
2.3k
Fork
546
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của JSONAPI-Resources/jsonapi-resources

Tất cả issue của JSONAPI-Resources/jsonapi-resources

Issue tương tự

Thêm issue về Ruby

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.