Tag: mixins

如何在factory_girl工厂中包含一个模块?

我正在尝试在我的所有工厂中重用一个辅助方法,但是我无法让它工作。 这是我的设置: 辅助模块(在spec / support / test_helpers.rb中) module Tests module Helpers # not guaranteed to be unique, useful for generating passwords def random_string(length = 20) chars = [‘A’..’Z’, ‘a’..’z’, ‘0’..’9′].map{|r|r.to_a}.flatten (0…length).map{ chars[rand(chars.size)] }.join end end end 工厂(在spec / factories / users.rb中) FactoryGirl.define do factory :user do sequence(:username) { |n| “username-#{n}” } password random_string password_confirmation { […]

rails,bootstrap,media-breakpoint-only

我花了一天的时间在网上试图让bootstrap的媒体断点 – 只在没有找到明确指示的情况下工作。 我有引导导航栏工作,所以,我怀疑,我已正确安装引导程序。 我想让以下代码工作: h1 { @include media-breakpoint-only(xs) { color: red; } @include media-breakpoint-only(sm) { color: green; } @include media-breakpoint-only(md) { color: blue; } @include media-breakpoint-only(lg) { color: yellow; } @include media-breakpoint-only(xl) { color: orange; } } 但是我不知道在哪里放置这些代码或者我需要做些什么来让媒体断点才能正常工作。 当我将上面的代码放在application.scss的末尾(见下文)时,我收到以下错误: 未定义的mixin’media-breakpoint-only’。 我的Gemfile是 source ‘https://rubygems.org’ git_source(:github) { |repo| “https://github.com/#{repo}.git” } ruby ‘2.5.1’ # Bundle edge Rails […]

在Object中的模块中混合会导致所有对象将该模块的实例方法作为单例方法inheritance

当我尝试将自己的行为添加到Object类时,我会得到在将模块混合到用户定义的类时不会发生的不良影响。 module Entity def some_instance_method puts ‘foo’ end def self.secret_class_method puts ‘secret’ end module ClassMethods def some_class_method puts ‘bar’ end end def self.included( other_mod ) other_mod.extend( ClassMethods ) end end 现在,我创建类Bob ,使其包含Entity 。 class Bob; include Entity; end; 这产生了期望和预期的输出: Bob.secret_class_method #=> NoMethodError Bob.some_instance_method #=> NoMethodError Bob.some_class_method #=> bar Bob.new.secret_class_method #=> NoMethodError Bob.new.some_instance_method #=> foo Bob.new.some_class_method […]

命名空间和Mixins

我正在尝试清理我们的命名空间。 基本上我们的设置有点像 class myClass include myModule1 include myModule2 @important_var #critical instance variable 基本上@important_var是一个telnet处理程序,几乎所有方法都需要它。 这适用于它现在设置的方式。 不幸的是myModule1和myModule2变得越来越大。 所以我继续遇到方法的命名空间冲突。 我喜欢用模块包装器访问方法,例如: myClass_instance.myModule1.a_method 但我无法弄清楚如何做到这一点或其他更干净的名字间距的想法?

通过混合模块来覆盖类的实例方法

给定A类和模块B,混合B的实例方法,使其覆盖A的相应实例方法。 module B def method1 “B\#method1” end def method2 “B\#method2” end end class A def method1 “A\#method1” end def method2 “A\#method2” end # include B does not override instance methods! # (module gets mixed into the superclass) end puts A.new.method1 # want it to print out “B#method1” puts A.new.method2 # want it to print out […]

Rails – 为什么我不能在我的测试中使用我在模块中创建的方法?

我在lib目录中创建了一个模块,我可以在我的Rails应用程序(添加include ModuleName之后)中自由调用它包含的各种方法,没有任何问题。 然而,当谈到测试时,他们抱怨没有这样的方法。 我尝试将模块包含在我的测试助手中,但没有运气。 谁能帮忙。 4) Error: test_valid_signup_redirects_user_to_spreedly(UsersControllerTest): NoMethodError: undefined method `spreedly_signup_url’ for SpreedlyTools:Module /test/functional/user_controller_test.rb:119:in `test_valid_signup_redirects_user_to_spreedly’ module SpreedlyTools protected def spreedly_signup_url(user) return “blahblah” end end class ApplicationController < ActionController::Base helper :all # include all helpers, all the time protect_from_forgery # See ActionController::RequestForgeryProtection for details include SpreedlyTools …. end ENV[“RAILS_ENV”] = “test” require File.expand_path(File.dirname(__FILE__) + “/../config/environment”) […]

Ruby Mixins和Instance变量

是否有将params传递给混合方法的最佳实践方法? 使用mixin的类可以设置混合方法所期望的实例变量,或者它可以将所有必需的参数作为参数传递给混合方法。 这样做的背景是我们有一个Rails控制器可以发布内容 – 但是其他控制器甚至模型都需要能够“充当发布者”所以我将控制器方法分解为一个模块,我将其混合为需要。 例如,这里是来自Rails Controller的代码,它需要“充当发布者”并且它调用混合方法question_xhtml()… def preview @person = Person.find params[:id] @group = Group.find params[:parent_id] @div = Division.find params[:div_id] @format = ‘xhtml’ @current_login = current_login xhtml = person_xhtml() # CALL TO MIXED-IN METHOD render :layout => false end 最终,question_xhtml需要所有这些东西! 这种方法是否合理,或者做得更好 def preview person = Person.find params[:id] group = Group.find params[:parent_id] div = Division.find […]

ruby中的模块和类变量范围

我仍然试图清楚地理解模块/类/实例变量…… 我的代码目前看起来像这样…… module Foo @@var1 ={} @@var2 =[] @@var3 = nil def m1(value) @@var2 << value end def m2(value) @@var1[@@var3]=value end end class Bar include Foo p @@var1 end class Bar2 include Foo p @var1 end 我正在尝试创建一个模块,其中包含每个类的行为方式的类范围配置。 配置存储在@@ var1和@@ var2中。 使用此代码,变量在包含该模块的所有类之间共享。 这不是期望的结果,我希望每个类都拥有自己的行为配置。 我还尝试创建一个包含模块的单个类,并创建变量,但模块无法访问变量。 module Foo def m1(value) @@var2 << value end def m2(value) @@var1[@@var3]=value end […]

使用Class vs Module在Ruby中打包代码

假设我有一堆没有持久状态的相关函数,比如字符串差异包中的各种操作。 我可以在类或模块中定义它们(使用self ),它们可以以完全相同的方式访问: class Diff def self.diff … def self.patch … end 要么 module Diff def self.diff … def self.patch … end 然后我可以做Diff.patch(…) 。 哪个’更好’(或’正确’)? 我需要将它们分组的主要原因是名称空间问题,常用函数名称都在其他地方使用。 编辑:将示例从矩阵更改为差异。 矩阵是一个可怕的例子,因为它确实有状态,每个人都开始解释为什么把它们写成方法而不是回答实际问题更好。 🙁

从模块中访问包含类的命名空间

我正在研究一个模块,除其他外,它将为你混合它的类添加一些通用的’finder’类型function。 问题:出于方便和美观的原因,我想在类之外包含一些function,与类本身在同一范围内。 例如: class User include MyMagicMixin end # Should automagically enable: User.name(‘Bob’) # Returns first user named Bob Users.name(‘Bob’) # Returns ALL users named Bob User(5) # Returns the user with an ID of 5 Users # Returns all users 我可以在这些方法中做function,没问题。 案例1( User.name(‘Bob’) )很简单。 但是,案例2-4需要能够在User之外创建新的类和方法。 Module.included方法允许我访问该类,但不允许访问其包含的范围。 在Class和Module上没有简单的“父”类型方法。 (对于命名空间,我的意思是,不是超类,也不是嵌套模块。) 我能想到的最好方法是在类的#name上进行一些字符串解析,以打破其命名空间,然后将字符串转换回常量。 但这看起来很笨拙,鉴于这是Ruby,我觉得应该有更优雅的方式。 有没有人有想法? 或者我只是为了自己的利益而过于聪明?