关于rails的关注点是个好主意:http://37signals.com/svn/posts/3372-put-chubby-models-on-a-diet-with-concerns
并且制作非常小的方法也是一个好主意,这些方法不属于公共API.没有使用顾虑,那些成为ruby类中的私有方法.
在Rails ActiveSupport :: Concern模块中创建私有方法是否有意义?如果是这样,私有工作是否对于关注定义中的常规实例方法和类方法?
ruby ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 activesupport-concern
在Ruby类定义中声明private/ 实际发生了什么protected?它们不是关键字,因此这意味着它们必须是方法调用,但我无法找到它们的定义位置.它们似乎没有记录.声明private/ protected方法的两种不同方式(如下所示)是否以不同方式实现?(第二种方式显然是方法调用,但这在第一种方式中并不那么明显.)
class Foo
private
def i_am_private; end
def so_am_i; end
end
class Foo
def i_am_private; end
def so_am_i; end
private :i_am_private, :so_am_i
end
Run Code Online (Sandbox Code Playgroud) 我正在使用带有FactoryGirl的Rails 4.1.7,Rspec 3.0.4和一个Postgresql ActiveRecord数据库。
我正在尝试测试一种找到关联模型的方法,然后更新该模型中的列。在这一点上,测试还很粗糙,但它使所有关联成为必要(我在撬动中检查)。调用该方法时,期望值将继续返回nil(默认情况下该列为nil,但应更新为日期时间)。下面是代码:
post_picture_spec.rb
require 'rails_helper'
RSpec.describe ModelName::PostPicture, :type => :model do
describe 'update_campaign method' do
context 'when the picture is tied to a campaign' do
before do
@campaign = FactoryGirl.create(:campaign)
@campaign.media << @picture1 = FactoryGirl.create(:picture, posted: true, time_scheduled: Date.parse('2014-07-15 18:00:00'), time_posted: Date.parse('2014-07-15 18:00:00'))
@campaign.media << @picture2 = FactoryGirl.create(:picture, posted: true, time_scheduled: Date.parse('2014-08-20 19:00:00'), time_posted: Date.parse('2014-08-20 19:00:00'))
end
it 'should update first item in campaign' do
# pending("FactoryGirl update issue")
ModelName::PostPicture.send(:update_campaign, @picture)
expect(@campaign.time_started).to eq(@picture.time_posted)
end
end
end
Run Code Online (Sandbox Code Playgroud)
图片工厂 …