小编khe*_*lll的帖子

使用authlogic rails插件在数据库中预填充管理员用户

我一直在使用Authlogic rails插件.我真正使用的是拥有一个可以编辑网站的管理员用户.它不是人们注册帐户的网站.我将最终使创建用户方法受到已登录用户的限制,但当然,当我清除数据库时我无法创建用户,因此我必须以某种方式预先填充它.我试图进行迁移以放置我创建的用户的转储,但这不起作用,看起来非常h​​acky.处理这个问题的最佳方法是什么?这很麻烦,因为密码被哈希,所以我觉得我必须创建一个然后拉出哈希条目...

ruby authentication ruby-on-rails admin authlogic

4
推荐指数
2
解决办法
3816
查看次数

直接在模型上调用rspec导致存根中的错误!方法

运行时:

rake spec:models
Run Code Online (Sandbox Code Playgroud)

一切都运作良好,但当我这样做

rspec spec/models/spot_spec.rb
Run Code Online (Sandbox Code Playgroud)

Spot.stub! :test1,我得到:

undefined method `stub!' for Spot:Class
Run Code Online (Sandbox Code Playgroud)

只有当我包含该存根时才会发生错误!线.

任何想法如何避免它?我想仅为特定模型运行规范.

更新:

使用Ruby 1.9.2和RSpec 2.4.0,这里是spot_spec.rb代码:

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe Spot do
  before(:all) do
    Spot.stub! :test1
    @spot = Spot.new
  end

  subject {@spot}

  describe "validations" do
    it { should validate_presence_of(:user) }
  end
end
Run Code Online (Sandbox Code Playgroud)

和spec_helper.rb:

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  config.mock_with :rspec
end
Run Code Online (Sandbox Code Playgroud)

ruby rspec ruby-on-rails

4
推荐指数
1
解决办法
1943
查看次数

Ruby String#to_class

从拍摄前一后进行一些修改,以回应sepp2k的关于命名空间的评论,我已经实现字符串#to_class方法.我在这里分享代码,我相信它可能会被重构,特别是"i"计数器.您的意见表示赞赏.

 class String
   def to_class
     chain = self.split "::"
     i=0
     res = chain.inject(Module) do |ans,obj|
       break if ans.nil?
       i+=1
       klass = ans.const_get(obj)
       # Make sure the current obj is a valid class 
       # Or it's a module but not the last element, 
       # as the last element should be a class
       klass.is_a?(Class) || (klass.is_a?(Module) and i != chain.length) ? klass : nil
     end
   rescue NameError
     nil
   end
 end

 #Tests that should be passed.
 assert_equal(Fixnum,"Fixnum".to_class)
 assert_equal(M::C,"M::C".to_class)
 assert_nil …
Run Code Online (Sandbox Code Playgroud)

ruby metaprogramming introspection

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

扩展uniq方法

这是Ruby 1.8问题:

我们都知道如何使用Array#uniq

[1,2,3,1].uniq #=> [1,2,3]
Run Code Online (Sandbox Code Playgroud)

但是我想知道我们是否可以以一种处理复杂对象的方式对其进行修补。当前行为是这样的:

[{"three"=>"3"}, {"three"=>"4"}, {"three"=>"3"}].uniq 
#=> [{"three"=>"3"}, {"three"=>"4"}, {"three"=>"3"}]
Run Code Online (Sandbox Code Playgroud)

请求的是:

[{"three"=>"3"}, {"three"=>"4"}, {"three"=>"3"}].uniq 
#=> [{"three"=>"3"}, {"three"=>"4"}]
Run Code Online (Sandbox Code Playgroud)

ruby monkeypatching unique

2
推荐指数
1
解决办法
745
查看次数

将XML集合(Pivotal Tracker故事)转换为Ruby散列/对象

我有一个XML格式的故事集.我想解析文件并将每个故事作为散列或Ruby对象返回,以便我可以进一步操作Ruby脚本中的数据.

Nokogiri是否支持此功能,还是有更好的工具/库可供使用?

XML文档具有以下结构,通过Pivotal Tracker的Web API返回:

<?xml version="1.0" encoding="UTF-8"?>
<stories type="array" count="145" total="145">
  <story>
    <id type="integer">16376</id>
    <story_type>feature</story_type>
    <url>http://www.pivotaltracker.com/story/show/16376</url>
    <estimate type="integer">2</estimate>
    <current_state>accepted</current_state>
    <description>A description</description>
    <name>Receivable index listing will allow selection viewing</name>
    <requested_by>Tony Superman</requested_by>
    <owned_by>Tony Superman</owned_by>
    <created_at type="datetime">2009/11/04 15:49:43 WST</created_at>
    <accepted_at type="datetime">2009/11/10 11:06:16 WST</accepted_at>
    <labels>index ui,receivables</labels>
  </story>
  <story>
    <id type="integer">17427</id>
    <story_type>feature</story_type>
    <url>http://www.pivotaltracker.com/story/show/17427</url>
    <estimate type="integer">3</estimate>
    <current_state>unscheduled</current_state>
    <description></description>
    <name>Validations in wizards based on direction</name>
    <requested_by>Matthew McBoggle</requested_by>
    <created_at type="datetime">2009/11/17 15:52:06 WST</created_at>
  </story>
  <story>
    <id type="integer">17426</id>
    <story_type>feature</story_type>
    <url>http://www.pivotaltracker.com/story/show/17426</url>
    <estimate type="integer">2</estimate>
    <current_state>unscheduled</current_state>
    <description>Manual payment needs …
Run Code Online (Sandbox Code Playgroud)

ruby xml hash nokogiri pivotaltracker

2
推荐指数
1
解决办法
1667
查看次数

从类变量引用静态方法

我知道有这样的情况是有线的但不知怎的,我有它:

class foo
  #static method
  @staticmethod
  def test():
    pass

  # class variable
  c = {'name' : <i want to reference test method here.>}
Run Code Online (Sandbox Code Playgroud)

它的方法是什么?

仅供记录:

我认为这应该被视为python最差的做法.如果有的话,使用静态方法并不是真正的pythoish方式......

python static-methods class-variables

2
推荐指数
1
解决办法
455
查看次数

某些方法的python backports

以下方法是否有任何backport用于python 2.4:

any, all, collections.defaultdict, collections.deque
Run Code Online (Sandbox Code Playgroud)

python methods backport

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

在jQuery中选择祖先的有效方法

给出以下HTML代码段:

<div class="image">
  <div class="placeholder">
    <a class="load iframe" href="https://contestapp.com/images/4"><img src="/uploads/image/image_file/4/thumb_he_sport_nutrition_for_active_kids_photo.jpg?1307271623" alt="Thumb_he_sport_nutrition_for_active_kids_photo"></a>
  </div>
  <div class="user">By </div>
  <div class="actions">
      <span>
        <a class="vote-link" href="/images/4/vote"></a>
      </span>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我有一个类投票链接的链接,我想选择链接a.load和a.load img,我怎么能通过jQuery有效地做到这一点?

jquery jquery-selectors

2
推荐指数
1
解决办法
312
查看次数

PHP 5.3更新日志?

任何人都可以为我指出一个好的(有例子)改变日志为PHP 5.3?我听到很多ppl正在进行代码迁移,我想知道它是什么新东西.

php changelog

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

jQuery .submit问题

我对jQuery有一点问题:我已经.submit()在表单上定义了,当我通过单击提交按钮提交表单时,偶数触发器和事情工作顺利,但是,当我使用时document.forms[0].submit(),事件没有得到触发,但表格提交!! 有什么理由吗?我怎么能克服这个?

javascript jquery form-submit onsubmit

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

在数组中分配范围

我需要在一个范围内创建一个数字数组,例如:

[1..5] 10次= [1,1,2,2,3,3,4,4,5,5]

[1..5] 5次= [1,2,3,4,5]

[1..5] 3次= [1,3,5]

def distribute(start_value, end_value, times, is_integer)
    array = Array.new(times-1)

    min_value = [end_value,start_value].min
    max_value = [end_value,start_value].max

    if max_value-min_value<times
      factor = (max_value-min_value).abs/(array.size).to_f
    else
      factor = (max_value-min_value).abs/(array.size-1).to_f
    end

    for i in 0..array.size
      v = [ [max_value, factor*(i+1)].min, min_value].max
      is_integer ? array[i] = v.round : array[i] = v
    end

    start_value < end_value ? array : array.reverse
  end
Run Code Online (Sandbox Code Playgroud)

分布式(1,5,10,真)=> [1,1,1,2,2,3,3,4,4,4] #WRONG应为[1,1,2,2,3,3, 4,4,5,5]

分布(5,1,5,真)=> [5,4,3,2,1] #OK

分发(1,5,3,真)=> [4,5,5] #WRONG应该是[1,3,5]

ruby refactoring distribution range

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

用于验证字符串类型的python正则表达式

我想用python做以下事情:

  1. 验证UTF8字符串是否为整数.
  2. 验证UTF8字符串是否为浮点数.
  3. 验证UTF8字符串的长度是否为(1-255).
  4. 验证UTF8字符串是否为有效日期.

我是python的新手,我相信这应该用正则表达式来完成,除了最后一个.非常感谢您的帮助!

python regex validation

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