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

no implicit conversion of Pathname into String #758

Closed
eni9889 opened this issue Feb 19, 2020 · 2 comments · Fixed by #848
Closed

no implicit conversion of Pathname into String #758

eni9889 opened this issue Feb 19, 2020 · 2 comments · Fixed by #848

Comments

@eni9889
Copy link

eni9889 commented Feb 19, 2020

Describe your problem here.

Commands

bundle exec rake annotate_models

example model below. Also, primary keys are uuid in case that matters

class Site < ApplicationRecord
  belongs_to :user
  validates :anchor_link, url: { allow_blank: true, allow_nil: true }

  after_create_commit :post_creation_callback

  def post_creation_callback
    Seo::ChooseAnchorTextJob.perform_later(self.class.to_s, id)
  end

  after_save do
    if saved_change_to_anchor_text? || saved_change_to_anchor_link?
      Seo::SyncAnchorTextJob.set(wait: 1.minutes).perform_later(self.class.to_s, self.id) rescue nil
    end
  end
end

Version

  • annotate version: 3.0.3
  • rails version: 5.2.3
  • ruby version: 2.5.1p57
@a0s
Copy link

a0s commented Mar 3, 2020

I've got same problem with namespaced models. Structure of files are:

/app/models/rosstat.rb                     # empty module Rosstat
/app/models/rosstat/people_income.rb       # empty module PeopleIncome
/app/models/rosstat/people_income/file.rb  # class Rosstat::PeopleIncome::File < ApplicationRecord
/app/models/rosstat/people_income/value.rb # class Rosstat::PeopleIncome::Value < ApplicationRecord

Error

Unable to annotate app/models/rosstat/people_income/value.rb: no implicit conversion of Pathname into String
Unable to annotate app/models/rosstat/people_income/file.rb: no implicit conversion of Pathname into String
Unable to annotate app/models/rosstat/people_income.rb: no implicit conversion of Pathname into String

Settings

Annotate.set_defaults(
      'active_admin'                => 'false',
      'additional_file_patterns'    =>  [],
      'routes'                      => 'false',
      'models'                      => 'true',
      'position_in_routes'          => 'before',
      'position_in_class'           => 'before',
      'position_in_test'            => 'before',
      'position_in_fixture'         => 'before',
      'position_in_factory'         => 'before',
      'position_in_serializer'      => 'before',
      'show_foreign_keys'           => 'true',
      'show_complete_foreign_keys'  => 'false',
      'show_indexes'                => 'true',
      'simple_indexes'              => 'false',
      'model_dir'                   => 'app/models',
      'root_dir'                    => '',
      'include_version'             => 'false',
      'require'                     => '',
      'exclude_tests'               => 'true',
      'exclude_fixtures'            => 'true',
      'exclude_factories'           => 'true',
      'exclude_serializers'         => 'true',
      'exclude_scaffolds'           => 'true',
      'exclude_controllers'         => 'true',
      'exclude_helpers'             => 'true',
      'exclude_sti_subclasses'      => 'false',
      'ignore_model_sub_dir'        => 'false',
      'ignore_columns'              => nil,
      'ignore_routes'               => nil,
      'ignore_unknown_models'       => 'false',
      'hide_limit_column_types'     => 'integer,bigint,boolean',
      'hide_default_column_types'   => 'json,jsonb,hstore',
      'skip_on_db_migrate'          => 'false',
      'format_bare'                 => 'true',
      'format_rdoc'                 => 'false',
      'format_yard'                 => 'false',
      'format_markdown'             => 'false',
      'sort'                        => 'false',
      'force'                       => 'false',
      'frozen'                      => 'false',
      'classified_sort'             => 'true',
      'trace'                       => 'false',
      'wrapper_open'                => nil,
      'wrapper_close'               => nil,
      'with_comment'                => 'true'
    )

Version

  • annotate (3.1.0)
  • rails (6.0.1)
  • ruby (2.6.3)

Temporary fix (for me)

Delete empty modul definitions (to prevent autoloading, eg /app/models/rosstat.rb and /app/models/rosstat/people_income.rb), and add self.table_name = "blablabal table name" in every deep model. After that gem works good.

Update

Ok, my problem was here

      # We cannot get loaded model when `model_path` is loaded by Rails
      # auto_load/eager_load paths. Try all possible model paths one by one.
      absolute_file = File.expand_path(file)
      model_paths =
        $LOAD_PATH.select { |path| absolute_file.include?(path) }
                  .map { |path| absolute_file.sub(path, '').sub(/\.rb$/, '').sub(/^\//, '') }

This code assumes that every element of $LOAD_PATH are String, but it weren't.
There are two options to fix this:

  1. Add .map(&:to_s) after $LOAD_PATH in gem's code
  2. Find something similar to config.eager_load_paths << Rails.root.join("lib") in our code and replace it with config.eager_load_paths << Rails.root.join("lib").to_s

But, it still doesn't work without self.table_name, agrrhh :(

@elliotcm
Copy link

@a0s Thanks for that, the eager_loads_paths issue was the cause for me. We should put in a PR for the gem for that.

Hamms added a commit to Hamms/annotate_models that referenced this issue Jan 26, 2021
…ring values

As currently implemented, `get_loaded_model` inspects the `$LOAD_PATH` global for path values when trying to find the path for a model file.  This would be fine, except that variable is affected by userspace, which means that it will sometimes contain non-string values, often Pathnames.  To avoid responding with the error `Unable to annotate #{model_path}: no implicit conversion of Pathname into String` in this situation, we simply add an explicit `to_s` call before performing string-specific operations.

Fixes ctran#758
Hamms added a commit to Hamms/annotate_models that referenced this issue Jan 26, 2021
…ring values

As currently implemented, `get_loaded_model` inspects the `$LOAD_PATH` global for path values when trying to find the path for a model file.  This would be fine, except that variable is affected by userspace, which means that it will sometimes contain non-string values, often Pathnames.  To avoid responding with the error `Unable to annotate #{model_path}: no implicit conversion of Pathname into String` in this situation, we simply add an explicit `to_s` call before performing string-specific operations.

Fixes ctran#758
ctran pushed a commit that referenced this issue May 10, 2021
…ring values (#848)

As currently implemented, `get_loaded_model` inspects the `$LOAD_PATH` global for path values when trying to find the path for a model file.  This would be fine, except that variable is affected by userspace, which means that it will sometimes contain non-string values, often Pathnames.  To avoid responding with the error `Unable to annotate #{model_path}: no implicit conversion of Pathname into String` in this situation, we simply add an explicit `to_s` call before performing string-specific operations.

Fixes #758
ocarta-l pushed a commit to ocarta-l/annotate_models that referenced this issue Jun 18, 2021
…ring values (ctran#848)

As currently implemented, `get_loaded_model` inspects the `$LOAD_PATH` global for path values when trying to find the path for a model file.  This would be fine, except that variable is affected by userspace, which means that it will sometimes contain non-string values, often Pathnames.  To avoid responding with the error `Unable to annotate #{model_path}: no implicit conversion of Pathname into String` in this situation, we simply add an explicit `to_s` call before performing string-specific operations.

Fixes ctran#758
dazralsky pushed a commit to dazralsky/annotate_models that referenced this issue Aug 21, 2023
…ring values (#848)

As currently implemented, `get_loaded_model` inspects the `$LOAD_PATH` global for path values when trying to find the path for a model file.  This would be fine, except that variable is affected by userspace, which means that it will sometimes contain non-string values, often Pathnames.  To avoid responding with the error `Unable to annotate #{model_path}: no implicit conversion of Pathname into String` in this situation, we simply add an explicit `to_s` call before performing string-specific operations.

Fixes ctran/annotate_models#758
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants