简体   繁体   中英

Rails : Include module in antother module

I am trying to include one active_support concern module in another module :

module Lms
  # import/sync methods for programs
  module Resource
    class ExceededResourceRemoteIdsLimit < StandardError;
      extend ActiveSupport::Concern
      class_methods do
        def absent_from_lms(account_id)
          remote_ids = all_lms_remote_ids(account_id)
          if remote_ids.count > 200
            raise ExceededResourceRemoteIdsLimit, "#{self.class} : #{remote_ids.count} ids received"
          end
            where(
              account_id: account_id
            ).where.not(
              lms_id: remote_ids
            )
          rescue ExceededResourceRemoteIdsLimit => e
            Sentry.capture_exception(e)
        end

        def all_lms_remote_ids(account_id)
          @all_lms_remote_ids ||= import_from_360(account_id).map(&:_id)
        end
      end
    end
  end
end


module Lms
  # import/sync methods for groups
  module Course
    extend ActiveSupport::Concern
    include Resource
    class_methods do
      def import_from_360(account_id = nil)
        ActsAsTenant.current_tenant = tenant(account_id) if account_id

        ActsAsTenant
          .current_tenant
          .lms_client
          .get_courses
      end

      def create_or_update_from_360(lms_course)
        find_by_lms_id_and_upsert(
          lms_course._id,
          lms_data: lms_course
        )
      end

      def find_by_lms_id_and_upsert(lms_id, attributes = {})
        course = find_by_lms_id(lms_id)
        unless course
          return create!(
            attributes.merge(
              yunoo_title: attributes[:lms_data].name
            )
          )
        end

        course.update!(attributes)
        course
      end
    end
  end
end

and eventually the Course module is included in the Course class :

class Course < ApplicationRecord
  include Lms::Course
end

I have tried several things to include LMS::Resource in LMS::Course :

include Resource

included do 
   include Resource
end

extend Resource 

However Im always getting NoMethodError: undefined method absent_from_lms' for klass` when I try to call this method from the Resource module on the Course class.

How should I go about this and manage to include this module in this other module ?

module Lms
  # import/sync methods for programs
  module Resource
    extend ActiveSupport::Concern

    class ExceededResourceRemoteIdsLimit < StandardError; end

    class_methods do
      def absent_from_lms(account_id)
        remote_ids = all_lms_remote_ids(account_id)
        if remote_ids.count > 200
          raise ExceededResourceRemoteIdsLimit, "#{self.class} : #{remote_ids.count} ids received"
        end
        where(
          account_id: account_id
        ).where.not(
          lms_id: remote_ids
        )
      rescue ExceededResourceRemoteIdsLimit => e
        Sentry.capture_exception(e)
      end

      def all_lms_remote_ids(account_id)
        @all_lms_remote_ids ||= import_from_360(account_id).map(&:_id)
      end
    end
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM