Ruby on rails 为什么不';t包含的模块方法是否覆盖以前包含的模块方法?
我希望有条件地重写gem中定义的方法。我的想法是有条件地包括一个带有我希望重写的方法的模块。但我发现,在我希望重写的模块之后简单地包含一个模块,并不像我预期的那样重写方法。请参见下面的示例: 以下是我想要覆盖gem方法的方法:Ruby on rails 为什么不';t包含的模块方法是否覆盖以前包含的模块方法?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我希望有条件地重写gem中定义的方法。我的想法是有条件地包括一个带有我希望重写的方法的模块。但我发现,在我希望重写的模块之后简单地包含一个模块,并不像我预期的那样重写方法。请参见下面的示例: 以下是我想要覆盖gem方法的方法: module SocializationsOverideConcern extend ActiveSupport::Concern def follow!(followable) Socialization::RedisStores::Follow.fo
module SocializationsOverideConcern
extend ActiveSupport::Concern
def follow!(followable)
Socialization::RedisStores::Follow.follow!(self, followable)
Socialization::ActiveRecordStores::Follow.follow!(self, followable)
end
def unfollow!(followable)
Socialization::RedisStores::Follow.unfollow!(self, followable)
Socialization::ActiveRecordStores::Follow.unfollow!(self, followable)
NotificationDismissalService.new.call(followable, user: self, dont_match_trigger: true, destroy: true)
end
def like!(likeable)
Socialization::RedisStores::Like.like!(self, likeable)
Socialization::ActiveRecordStores::Like.like!(self, likeable)
end
def unlike!(likeable)
Socialization::RedisStores::Like.unlike!(self, likeable)
Socialization::ActiveRecordStores::Like.unlike!(self, likeable)
end
end
以下是我的类定义:
class User < ActiveRecord::Base
acts_as_liker
acts_as_likeable
acts_as_follower
acts_as_followable
acts_as_mentionable
include SocializationsOverideConcern if $rollout.active?(:redis_to_mysql)
class用户
方法如下!,放开!在社会化gem中定义了等。并通过上述acts_包括在内
不管怎么说,这就是我想要实现的目标,覆盖下面的内容!,放开!,喜欢而且不像!方法如果“redis_to_mysql卷展栏处于活动状态
如何实现这一点?如果条件在应用程序初始化后的某个时间点变为真,则在条件变为真后,
发送
用户
方法。e、 g
def activate_rollout
$rollout.activate(:redis_to_msql)
User.send(:include, SocializationsOverideConcern)
end
$rollout.active?(:redis_to_mysql) # => false
User.include? SocializationsOverideConcern # => false
activate_rollout
$rollout.active?(:redis_to_mysql) # => true
User.include? SocializationsOverideConcern # => true
另一种方法是在方法体中添加条件并使用super
module SocializationsOverideConcern
extend ActiveSupport::Concern
def rollout_active?
$rollout.active?(:redis_to_mysql)
end
def follow!(followable)
return super unless rollout_active?
Socialization::RedisStores::Follow.follow!(self, followable)
Socialization::ActiveRecordStores::Follow.follow!(self, followable)
end
def unfollow!(followable)
return super unless rollout_active?
Socialization::RedisStores::Follow.unfollow!(self, followable)
Socialization::ActiveRecordStores::Follow.unfollow!(self, followable)
NotificationDismissalService.new.call(followable, user: self, dont_match_trigger: true, destroy: true)
end
def like!(likeable)
return super unless rollout_active?
Socialization::RedisStores::Like.like!(self, likeable)
Socialization::ActiveRecordStores::Like.like!(self, likeable)
end
def unlike!(likeable)
return super unless rollout_active?
Socialization::RedisStores::Like.unlike!(self, likeable)
Socialization::ActiveRecordStores::Like.unlike!(self, likeable)
end
end
class User < ActiveRecord::Base
acts_as_liker
acts_as_likeable
acts_as_follower
acts_as_followable
acts_as_mentionable
include SocializationsOverideConcern
end
module socializationsoveridoccern
扩展ActiveSupport::关注点
def卷展栏处于活动状态?
$rollout.active?(:redis_to_mysql)
结束
跟我来!(续)
返回super,除非您处于活动状态?
社会化::再贴现商店::跟随。跟随!(自我,可跟随)
社会化::ActiveRecordStores::Follow.Follow!(自我,可跟随)
结束
放轻松!(续)
返回super,除非您处于活动状态?
社会化::RedisStores::Follow.unfollow!(自我,可跟随)
社会化::ActiveRecordStores::Follow.unfollow!(自我,可跟随)
NotificationDismissalService.new.call(可跟踪,用户:self,不匹配\u触发器:true,销毁:true)
结束
我喜欢!(讨人喜欢)
返回super,除非您处于活动状态?
社会化::商店::喜欢。喜欢!(自我,讨人喜欢)
社会化::ActiveRecordStores::Like.Like!(自我,讨人喜欢)
结束
不象!(讨人喜欢)
返回super,除非您处于活动状态?
社会化::RedisStores::喜欢。不喜欢!(自我,讨人喜欢)
社会化::ActiveRecordStores::Like.inspect!(自我,讨人喜欢)
结束
结束
类用户
这样,模块总是被包括在内,但条件会移动到方法执行中。当条件返回到
false
时,如果您需要能够恢复默认功能,这可能更可取。两个问题:1)您确定$rollout.active吗?(:redis_to_mysql)
是真的吗?2) 当它是真实的,并且您调用User.concents
时,它会输出什么?如果$rollout.active,它会包括socializationsoveridoccern吗?(:redis_to_mysql)
实际上是无效语法。我添加它只是为了显示意图。当我选中User.祖先时,我要覆盖的模块显示在我要覆盖它们的模块之后。如果条件无效,则不确定为什么…包含模块。你是这么说的吗?或者你是在谈论$rollout.active?(:redis_to_mysql)
位?你能澄清一下你所说的我想要覆盖的模块在我想要覆盖它们的模块之后显示吗?这意味着它们在数组中有更高的索引?