0

Пишу плагин на Redmine.

Структура:

app 
  models
    telegram_account.rb
lib
  my_controller_patch.rb
init.rb

app/models/telegram_account.rb

require "httparty"
require "json"
module MyControllerPatch
    class TelegramAccount < ActiveRecord::Base
    end
end

lib/my_controller_patch.rb

module MyControllerPatch
    def self.included(base)
        base.extend(ClassMethods)
        base.send(:include, InstanceMethods)

        base.class_eval do
            alias_method :account, :telegram_account
        end
    end

    module ClassMethods
    end

    module InstanceMethods
        def telegram_account
            @user = User.current
            @pref = @user.pref

            if ( TelegramAccount.find(user_id: User.current.id).present? )
                @telegram = TelegramAccount.find(user_id: User.current.id)
            else
                @telegram = TelegramAccount.new
            end
        end

        def referrer
            puts request.domain
        end
    end
end

init.rb

require('hooks')

Rails.application.config.to_prepare do
    MyController.send(:include, MyControllerPatch)
end

Redmine::Plugin.register :my_telegram_plugin do
  name 'Some plugin'
  author 'Some'
  description 'This is a plugin for Redmine'
  version '0.0.1alpha'
  url 'http://site.local'
  author_url 'http://site.local/about'

  settings :default => {
      :data => "",
  }, :partial => 'settings/index'
end

Получаю ошибку:

Started GET "/my/account" for 192.168.195.1 at 2019-12-25 10:00:12 +0800
Processing by MyController#account as HTML
  Current user: admin (id=1)
Completed 500 Internal Server Error in 74ms (ActiveRecord: 38.0ms)

NameError (uninitialized constant MyControllerPatch::InstanceMethods::TelegramAccount):
  plugins/garmayev-telegram-chat/lib/my_controller_patch.rb:19:in `telegram_account'
  lib/redmine/sudo_mode.rb:63:in `sudo_mode'

redmine 3.4.13 rails 4.2

Как я понял, ruby не может найти модель. Вопрос: почему?

На версии redmine 4.0 с его rails 5 этот плагин (по крайней мере патчинг контроллера) срабатывает, а вот при переходе на нижнюю версию redmine - эта ошибка.

P.S.: на руби еще новичок, если есть банальные ошибки - ткните носом

3
  • 1
    Попробуйте внутри метода telegram_account обращаться к модели через ::TelegramAccount (обратите внимание на два двоеточия в начале) 25 дек 2019 в 3:10
  • @Василиса Все равно та же самая ошибка NameError (uninitialized constant TelegramAccount): plugins/garmayev-telegram-chat/lib/my_controller_patch.rb:19:in telegram_account' lib/redmine/sudo_mode.rb:63:in sudo_mode'
    – garmayev
    25 дек 2019 в 3:32
  • Невнимательно прочитала вопрос, он же у вас в модуль завернут. Правильно будет ::MyControllerPatch::TelegramAccount 25 дек 2019 в 9:34

1 ответ 1

0

Попробуйте изменить структуру, вместо

app
  models
    telegram_account.rb

добавить папку

app
  models
    my_controller_patch
      telegram_account.rb

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.