开发者

Rails 3 : Add some methods in models, for development

开发者 https://www.devze.com 2023-02-27 22:17 出处:网络
I would like to add some methods to some AR Models of my App; but I think they should only be available und开发者_StackOverflower some circumstances; this requires some meta-programming.

I would like to add some methods to some AR Models of my App; but I think they should only be available und开发者_StackOverflower some circumstances; this requires some meta-programming.

So I'd like to have a file where I put all my debug methods, the only question is where to put it?

Example, I have the models:

class Admin::Restaurant < ActiveRecord::Base 
class Admin::Order < ActiveRecord::Base 

And in my file I have (it does deppend on MetaWhere.operator_overload! initialization):

if Rails.env != 'production'

  class Admin::Order

    def self.mock_make
      r = Restaurant.first
      user_query = User.where( :created_at > "2011-04-01"  )
      u = user_query.first( :offset => ( user_query.count * rand ).to_i )

      o = r.orders.new
      o.user = u
      o.value = rand(100) + rand.round(2)

      if o.save
        return o
      else
        return nil
      end
    end

  end

end

The thing is.. I can't get it to work on /config/initializers or /app/models.


Wrap it as a external module and include it with if condition

class MyClass << ActiveRecord::Base
  include MyExtraModule if Rails.env == 'development'
end


Put them in config/environments/development.rb

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号