Skip to content

Instantly share code, notes, and snippets.

@kurko
kurko / gist:2388390
Created April 14, 2012 22:55
Solr Unknown Field
# It was working normally. I had added some dynamic fields to solr/conf/schema.xml. Then I git cloned again (after removing the project directory), same machine, and started getting this errors:
RSolr::Error::Http - 400 Bad Request
Error: ERROR:unknown field 'name_textp'
Request Data: "<?xml version=\"1.0\" encoding=\"UTF-8\"?><add><doc><field name=\"id\">Good 12</field><field name=\"type\">Good</field><field name=\"type\">ActiveRecord::Base</field><field name=\"class_name\">Good</field><field name=\"company_id_i\">1</field><field name=\"name_textp\">hber</field></doc></add>"
Backtrace: /Users/kurko/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rsolr-1.0.7/lib/rsolr/client.rb:230:in `adapt_response'
/Users/kurko/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rsolr-1.0.7/lib/rsolr/client.rb:167:in `execute'
/Users/kurko/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rsolr-1.0.7/lib/rsolr/client.rb:161:in `send_and_receive'
class PostCommitmentContext
def initialize(params)
@params = params
end
def save
@post = Post.new(@params)
if @post.save
# @post é ActiveRecord, então não há o que extender mesmo porque ActiveRecord já está encruado
@twitter = Twitter.login("username", "password")
# A controller
class PostsController < ActionController::Base
def create
@context = PostManagementContext.new(params[:posts])
if @context.save
# redirect
end
end
def search
@kurko
kurko / gist:2410930
Created April 18, 2012 03:35
DCI in real code
class ReceivablesManagementContext
def initialize(params)
@params = params
end
def save_receivable
sanitize_controller_params
@receivable = AccountReceivable.new(@params[:account_receivable])
@receivable.customer_id = @params[:customer_id]
@receivable.save
# no terminal, rodar:
#
# ruby -v
#
# retorna 1.8.7 ou 1.9.x
array_one = []
puts array_one.count # => 0
array_two = [:first, :second, :third]
proxies.each do |method_name, dependency|
if model.class.ancestors.include?(dependency) || dependency.nil?
class << self
# The line below raises:
# undefined method `method_name' for Product:Class
# class << self is changing the scope, how can I get `method_name` from
# the each iterator?
self.send(:define_method, method_name) do |*args, &block|
model.send(method_name, *args, &block)
end
module Accounts
class Company
def my_method
end
end
class Person
def whatever
1 # Rails
2 module ActiveRecord
3 class Base
4 def created_at
5 "21/04/1987"
6 end
7 end
8 end
9
10 # model
@kurko
kurko / gist:2936856
Created June 15, 2012 14:55
Bank, Tranfers and accounts
class Bank
def transfer(from, to, amount)
transfer = Transfer.new(from, to, amount)
transfer.transfer
end
end
class Transfer
def initialize(from, to, amount)
@from = from
@kurko
kurko / unit_testing.rb
Created June 16, 2012 23:18
Unit test example
class Car
def run
"The car is running"
end
end
# RSpec example
describe "#run" do
it "should run" do
Car.new.run.should == "The car is running"