class User < ActiveRecord::Base def name [ first_name, last_name ].filter(&:presence).compact.map(&:strip) * ' ' end end
class User < ActiveRecord::Base def self.find_old where('created_at < ?', 1.year.ago).all end end
class User < ActiveRecord::Base attr_accessor :foo has_many :authentications, :dependent => :destroy has_many :invitees, :class_name => 'User', :as => :invited_by delegate :city, :country, :to => :location attr_accessible :admin, :banned, :as => :admin mount_uploader :userpic, UserPicUploader scope :admin, where(:admin => true) validates :username, :uniqueness => true, :format => /^[az][az\d_-]+$/, :length => { :within => 3..20 }, :exclusion => { :in => USERNAME_EXCLUSION } end
# user_stuff.rb module UserStuff def name [ first_name, last_name ].filter(&:presence).compact.map(&:strip) * ' ' end end # user.rb class User < ActiveRecord::Base include UserStuff end
# user_stuff.rb module UserStuff def self.find_old where('created_at < ?', 1.year.ago).all end end # user.rb class User < ActiveRecord::Base include UserStuff end
find_old
method for the find_old
module UserStuff
, and it will not end up in the model. # user_stuff.rb module UserStuff def find_old where('created_at < ?', 1.year.ago).all end end # user.rb class User < ActiveRecord::Base extend UserStuff # , include, extend end
# user_stuff.rb module UserStuff module InstanceMethods def name [ first_name, last_name ].filter(&:presence).compact.map(&:strip) * ' ' end end module ClassMethods def find_old where('created_at < ?', 1.year.ago).all end end end # user.rb class User < ActiveRecord::Base include UserStuff::InstanceMethods extend UserStuff::ClassMethods end
has_many
etc. Therefore, it is necessary to shove the code into the module in such a way that it is executed only when this module is connected to the model. Fortunately, it’s very easy to cut. module UserValidations def self.included(base) # base — , . base.instance_eval do # base # validates :username, :uniqueness => true, :format => /^[az][az\d_-]+$/, :length => { :within => 3..20 }, :exclusion => { :in => USERNAME_EXCLUSION } validates :gender, :allow_blank => true, :inclusion => { :in => %w(mf) } end end end
# user_stuff.rb module UserStuff def self.included(base) # base.extend ClassMethods # # Module#include — . # base.include(InstanceMethods), : base.send :include, InstanceMethods # base.instance_eval do validates :gender, :presence => true end end # module ClassMethods def find_old where('created_at < ?', 1.year.ago).all end end # - module InstanceMethods def name [ first_name, last_name ].filter(&:presence).compact.map(&:strip) * ' ' end end end # user.rb class User < ActiveRecord::Base include UserStuff end
ActiveSupport::Concern
.ActiveSupport::Concern
our code can be rewritten as follows: module UserStuff extend ActiveSupport::Concern included do validates :gender, :presence => true end # module ClassMethods def find_old where('created_at < ?', 1.year.ago).all end end # - module InstanceMethods def name [ first_name, last_name ].filter(&:presence).compact.map(&:strip) * ' ' end end end
lib/active_support/concern.rb
, there are very cool and spreading comments with code examples and all that. # lib/models/validations.rb module Models module Validations # c end end # lib/models/user/validation.rb module Models module User module Validations # User end end end # app/models/user.rb class User < ActiveRecord::Base include Models::Validations include Models::User::Validations end
Models::User::Validations
, first tries to search for the file models/user/validations.rb
and try to load it , and only then, in case of failure, panic and throw a NameError
exception.instance_eval
. Code corrected. Comrade plus karma for attentiveness.Source: https://habr.com/ru/post/125822/
All Articles