我有点不知所措.Rails 3中的Scopes的单元测试是否有意义?
一方面,我正在编写代码,我应该测试该代码.
但是,另一方面,基本上我的所有范围都是微不足道的.检查传递参数的一个变量几乎是我迄今为止最复杂的范围.
scope :author, proc { |author| where(:author_user_id => author }
该代码是微不足道的,并且或多或少地覆盖了实际使用范围的函数.
测试或不测试范围的最佳实践是什么?
我有一个作为过滤器的范围.例如:
class User
scope :in_good_standing, -> { where(:some_val => 'foo', :some_other_val => 'bar' }
end
Run Code Online (Sandbox Code Playgroud)
因为in_good_standing依赖于多个条件,我想在以下实例上User定义:
def in_good_standing?
some_val == 'foo' && some_other_val == 'bar'
end
Run Code Online (Sandbox Code Playgroud)
但是,我真的想避免重复实例方法和命名范围之间的逻辑.有没有办法以#in_good_standing?简单引用范围的方式定义?
我意识到这些是非常不同的概念(一个是类方法,一个是实例方法),因此我的问题.正如@MrDanA在评论中提到的,我能得到的最接近的是检查我好奇的记录是否存在于更大的范围内,这可能是我正在寻找的答案.
关于从我的示例中分离出不同范围的响应是有用的,但我正在寻找一种适用于应用程序的通用模式,其中一些非常复杂的逻辑由作用域驱动.
我有named_scope,它在多个ActiveRecord模型中重用.例如:
named_scope :limit, lambda {|limit| {:limit => limit}}
Run Code Online (Sandbox Code Playgroud)
提取此代码以在模型之间共享的最佳实践是什么?是否可以将其提取到模块或者我是否应该重新打开ActiveRecord :: Base类?
using Statement (C# Reference)
Defines a scope, outside of which an object or objects will be disposed.
但我得到了一些用户在这里发布的代码,我对此感到困惑:(请参阅我对代码的评论)
using (OleDBConnection connection = new OleDBConnection(connectiongString))
{
if (connection.State != ConnectionState.Open)
connection.Open();
string sql = "INSERT INTO Student (Id, Name) VALUES (@idParameter, @nameParameter)";
using (OleDBCommand command = connection.CreateCommand())
{
command.CommandText = sql;
command.CommandType = CommandType.Text;
OleDBParameter idParameter = command.CreateParameter();
idParameter.DbType = System.Int32;
idParameter.Direction = Parameterdirection.Input;
idParameter.Name = "@idParameter";
idParameter.Value = studentId;
OleDBParameter nameParameter = command.CreateParameter();
try
{
command.ExecuteNonQuery();
} …Run Code Online (Sandbox Code Playgroud) 在我的Rails3模型中,我有两个命名范围:
scope :within_limit, where("wait_days_preliminary <= ? ", ::WAIT_TIME_LIMIT.to_i )
scope :above_limit, where("wait_days_preliminary > ? ", ::WAIT_TIME_LIMIT.to_i )
Run Code Online (Sandbox Code Playgroud)
基于它们的相似性,我很自然地通过反转第一个来定义第二个.
我怎么能在Rails中做到这一点?
我想知道是否有办法在named_scope中使用"find_by_sql".我想将自定义sql视为named_scope,因此我可以将其链接到我现有的named_scopes.优化我经常使用的sql片段也很有用.
有没有办法以兼容的方式组合范围?
如果我有范围
User.big_haired
Run Code Online (Sandbox Code Playgroud)
和
User.plays_guitar
Run Code Online (Sandbox Code Playgroud)
我可以打电话
User.big_haired.plays_guitar
Run Code Online (Sandbox Code Playgroud)
并让所有拥有大发和弹吉他的用户.我可以写这个来让所有有头发或弹吉他的用户?
我想我必须补充一点,我意识到你可以运行一组查询并将结果添加到一起.这就是我不想做的事情.
我正在使用Rails gem rails3-jquery-autocomplete来为帖子添加类别.
我想限制搜索只包括属于当前用户或帖子的作者的类别在结果中.
文档说我可以指定范围:
:示波器
添加了使用范围的选项.传递数组中的范围.例如:scopes => [:scope1,:scope2]
但我不确定如何在这里传递用户ID?
这似乎是一个comon场景,我错过了一些明显的东西吗?
我找到了一个建议修改get_item方法的答案,但这似乎打破了自动完成
ruby named-scope ruby-on-rails jquery-autocomplete ruby-on-rails-3
是否可以named_scope为某列提供唯一的返回记录?
例如
named_scope :unique_styles, :order =>"title desc", :limit => 3
Run Code Online (Sandbox Code Playgroud)
这会给我三种风格,但如果我想确定标题不同怎么办?在这种情况下,可能有三个具有相同样式的记录,我希望这个named_scope只给出标题的唯一值.
所以["style 1", "style 1", "style 1"]不可能,它会强迫自己给予["style 1", "some style 2", "maybe another 3"]
group可能会这样做,而我现在正在使用它.如果有人有任何评论,不管这是不是很好.是的,我知道之前已经问过,是的,我知道这是"按设计".
但是我想做这样的事情:
@Component(modules = {RealmModule.class})
public interface RealmComponent {
Realm realm();
}
@Component(modules = {RepositoryModule.class})
public interface RepositoryComponent {
PersonRepository personRepository();
ScheduleRepository schedulesRepository();
}
@Component(dependencies = {RealmComponent.class, RepositoryComponent.class})
public interface AppDataComponent
extends RealmComponent, RepositoryComponent {
}
@ApplicationScope
@Component(dependencies = {AppContextComponent.class,
AppDataComponent.class,
AppDomainComponent.class,
AppPresentationComponent.class,
AppUtilsComponent.class})
public interface ApplicationComponent
extends AppContextComponent, AppDataComponent, AppDomainComponent, AppUtilsComponent, AppPresentationComponent {
void inject(CustomApplication customApplication);
void inject(DashboardActivity dashboardActivity);
}
Run Code Online (Sandbox Code Playgroud)
但是,我得到的是无范围的,每次我注入一个JobManager或一个ScheduleRepository或其他任何东西,我得到一个新的实例.我唯一可以"修复"的方法就是这样.
@Module
public class JobManagerModule {
private JobManager jobManager;
@Provides
public …Run Code Online (Sandbox Code Playgroud) named-scope ×10
activerecord ×4
android ×1
c# ×1
dagger-2 ×1
ruby ×1
scope ×1
sql ×1
unit-testing ×1
using ×1