小编mai*_*ald的帖子

返回满足谓词的map/list/sequence中的第一项

我正在寻找一个函数,它返回序列中的第一个元素,其中fn的计算结果为true.例如:

(first-map (fn [x] (= x 1)) '(3 4 1))
Run Code Online (Sandbox Code Playgroud)

上面的假函数应该返回1(列表中的最后一个元素).在Clojure中有类似的东西吗?

clojure

49
推荐指数
6
解决办法
3万
查看次数

动态添加角色给用户

我们使用Symfony2的角色功能来限制用户访问我们应用的某些部分.我们的每个用户实体都有许多具有开始日期和结束的订阅实体,用户可以购买年度订阅.

现在,有没有办法根据用户是否拥有"有效"订阅动态添加角色?在rails中,我只是让模型处理它是否具有必要的权限,但我知道通过设计symfony2实体不应该访问Doctrine.

我知道你可以从一个实体实例中访问一个实体的关联,但是它会遍历所有用户的订阅对象,这对我来说似乎不那么麻烦.

user-roles symfony

15
推荐指数
1
解决办法
7361
查看次数

使用rspec测试rails的嵌套属性

我对测试和rails非常陌生,并试图自己解决这个问题,但没有运气.

我有以下型号

class Picture < ActiveRecord::Base
  belongs_to :product
  has_attached_file :image
end

class Product < ActiveRecord::Base
  has_many :pictures, :dependent => :destroy
  accepts_nested_attributes_for :pictures, :reject_if => lambda { |p| p[:image].blank? }, :allow_destroy => true
end
Run Code Online (Sandbox Code Playgroud)

和一个相当标准的控制器,我猜......

def create
  @product = Product.new(params[:product])
  if @product.save
    redirect_to products_path, :notice => "blah."
  else
    render :action => "new"
  end
end
Run Code Online (Sandbox Code Playgroud)

我将如何进行测试呢?我试过这样的东西,但是我无法让它起作用:

describe ProductsController do
  it "adds given pictures to the product" do
    product = Factory.build(:product)
    product.pictures.build(Factory.attributes_for(:picture))
    post :create, :product => product.attributes
    Product.where(:name => product[:name]).first.pictures.count.should == 1 # …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails

6
推荐指数
2
解决办法
5498
查看次数

标签 统计

clojure ×1

rspec ×1

ruby-on-rails ×1

symfony ×1

user-roles ×1