代码之家  ›  专栏  ›  技术社区  ›  Nilay Singh

如何调用Rails模型中的可重用代码?

  •  0
  • Nilay Singh  · 技术社区  · 6 年前

    我在我的模型中有一个重复的方法,我想把这些代码放在一个地方,并且只想访问到我的模型中。

    我的模型有以下几种方法:

    class ProductionProductivity7 < ApplicationRecord
    def self.import1(file)
      spreadsheet = Roo::Spreadsheet.open(file.path)
            header = spreadsheet.row(1)
            (2..spreadsheet.last_row).each do |i|
              row = Hash[[header, spreadsheet.row(i)].transpose]
              puts row.to_hash
              product = find_by(id: row["id"]) || new
              product.attributes = row.to_hash
              product.save!
        end
    end
    def self.search(search,compare)
      if search == "All"
        all.order(:id)
      elsif compare == "Bihar vs District"
        where("Districts = ? OR Districts = ?", search, "Bihar")
      else
        where(Districts: search)
      end
    end
    

    结束

    还有3个类似的方法,我想把这个代码块放到helper中,只想在模型内部调用它。为此,我尝试了把这些代码放到我的助手中。我称之为:

    include ApplicationController.ProductionProductivity7sHelper
    

    我在我的模型中包含了这个,但是我得到了这个错误:

    undefined method `ProductionProductivity7sHelper' for ApplicationController:Class
    

    我的控制器代码如下:

     def test
          @ProductionProductivity7s = ProductionProductivity7.search(params[:search],compare)
          a = ProductionProductivity7.query(@ProductionProductivity7s,params[:year],rain_fall_type,views,compare)
     end 
    

    我在app文件夹中添加了一个module name code.rb。

       module Code
        def search(search_scope,compare)
            if search_scope == "All"
            all.order(:id)
            elsif compare == "Bihar vs District"
            where("Districts = ? OR Districts = ?", search_scope, "Bihar")
            else
            where(Districts: search_scope)
            end
        end
    end
    

    My controller and Model code link

     undefined method `search' for #<Class:0x00007ff115974fd8> Did you mean? search1
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   jvillian    6 年前

    module Import1
    
      def import1(file)
        spreadsheet = Roo::Spreadsheet.open(file.path)
        header = spreadsheet.row(1)
        (2..spreadsheet.last_row).each do |i|
          row = Hash[[header, spreadsheet.row(i)].transpose]
          puts row.to_hash
          product = find_by(id: row["id"]) || new
          product.attributes = row.to_hash
          product.save!
        end
      end
    
      def search(search_scope,compare)
        if search_scope == "All"
          all.order(:id)
        elsif compare == "Bihar vs District"
          where("Districts = ? OR Districts = ?", search_scope, "Bihar")
        else
          where(Districts: search_scope)
        end
      end
    
    end
    

    app models app/model_modules app/shared_model_modules import_1.rb

    class ProductionProductivity7 < ApplicationRecord
      extend Import1
    end
    

    BaseService

    class BaseService
    
      attr_accessor :args
    
      class << self
    
        def call(args=nil)
          new(args).call
        end
    
      end # Class Methods
    
      #=======================================================================
      # Instance Methods
      #=======================================================================
    
        def initialize(args)
          @args = args || {}
          assign_args
        end
    
      private
    
        def assign_args
          args.each do |k,v|
            class_eval do 
              attr_accessor k
            end
            send("#{k}=",v)
          end
        end
    
    end
    

    class ImportFileService < BaseService
    
      def call
        spreadsheet = Roo::Spreadsheet.open(file.path)
        header = spreadsheet.row(1)
        (2..spreadsheet.last_row).each do |i|
          row = Hash[[header, spreadsheet.row(i)].transpose]
          puts row.to_hash
          product = klass.find_or_initialize_by(id: row["id"])
          product.attributes = row.to_hash
          product.save!
        end
      end
    
    end
    

    ImportFileService.call(file: file, klass: ProductionProductivity7)
    
        2
  •  0
  •   jesellers    6 年前

    module ProductionProductivity7sHelper
      def import1(file) # notice I dropped 'self.'
    
        ...
    
      end
    
      ...
    
    end
    

    class ProductionProductivity7 < ApplicationRecord
      extend ProductionProductivity7sHelper
    
      ...
    
    end