Posts

Showing posts with the label ruby

Minitest - calling a mocked instance method from within another instance method

Minitest - calling a mocked instance method from within another instance method I've come across some weird behavior in Minitest::Mock and can't figure out what's the reason behind it. Minitest::Mock Say I have this class A that has a method b that calls method c : b c class A def b c end def c 1 end end I would like to mock the c method to return 2 instead of 1: c require 'minitest/autorun' a = Minitest::Mock.new(A.new) a.expect(:c, 2) But for some reason b still returns 1: b > a.b => 1 Obviously calling c directly will work: c > a.c => 2 > a.c MockExpectationError: No more expects available for :c: Why is the expectation on c not being invoked when calling it from within the instance? c 1 Answer 1 This is how Minitest::Mock works; the mock object is a wrapper around the "real" instance of A , and defined expectations intercept the...

Optimize with joins, order, distinct on current active records / sql query

Optimize with joins, order, distinct on current active records / sql query I am using ruby on rails 4 and postgres currently, and making distinct and sorting as in the code below. I just have a feeling is not right and not optimized because it seems to be doing all this with an array. How could I combine this all into one activerecords query ? ads1 = Advertisement.where(property_id: nil) .where(target_address_city: [property.address_city, ""], target_address_state: [property.address_state, ""], target_address_country_id: property.address_country_id) # return advertisement belongs to the property ads2 = management.property.advertisements #combine both results combine_ads = ads1 + ads2 #remove ads duplicate uniq_ads = combine_ads.uniq { |ads| ads.id} # sort by created_at desc uniq_ads = uniq_ads.sort_by { |ads| -ads[:id]} # do pagination final_ads = uniq_ads.paginate(:page => pa...

ruby `rescue in block in connect': Failed to open TCP connection to api

ruby `rescue in block in connect': Failed to open TCP connection to api I am fairly new to ruby and I am having issues using code that I found on this github. It is meant to use the Whitepages API to use their service and I am using it to do a reverse phone lookup. Here is my whitepages.rb file: require 'json' require 'net/https' require 'uri' class Whitepages #Initialize the Whitepages class # * api_key - The API key obtained from the Whitpages Developer website def initialize(api_key) api_version = "1.0" @api_key = api_key @base_uri = "http://api.whitepages.com/" @find_person_uri = @base_uri + "find_person/" + api_version + "/?" @reverse_phone_uri = @base_uri + "reverse_phone/" + api_version + "/?" @reverse_address_uri = @base_uri + "reverse_address/" + api_version + "/?" @uri = URI.parse(@base_uri) @http = Net::HTTP.new(@uri.host, @uri.port) end #Retrieves cont...

Rails 5: Is there a way to run a single set of tests against the development database?

Rails 5: Is there a way to run a single set of tests against the development database? I would like to run a single set of tests against the development database. My seeds.rb file populates the databse from a CSV and I want to ensure that the data is stored in the database in the way I expect. I don't want to run all tests against the development database, just a particular set. seeds.rb I created an integration test. I thought I could switch environments in #setup but it looks like Rails.env = 'development' has no effect. #setup Rails.env = 'development' require 'test_helper' class DbTest < ActionDispatch::IntegrationTest def setup Rails.env = 'development' end def test_total_settlements ... Is it possible to run tests in different environments? If so, how is this done? Why not seeds the test database and check how it's populated ? – Зелёный Jul 1 at 5:34 ...

Bundler could not find compatible versions for multiple gems that seem to fall within range of requirements

Bundler could not find compatible versions for multiple gems that seem to fall within range of requirements I'm trying to use a gem spree , which depends on kaminari (~> 1.0.1) . In my Gemfile.lock , I have kaminari (= 1.1.1) . This satisfies the requirement for spree , since it's greater than or equal to the last digit. However, I'm getting this error when I try to bundle: spree kaminari (~> 1.0.1) Gemfile.lock kaminari (= 1.1.1) spree Bundler could not find compatible versions for gem "kaminari": In snapshot (Gemfile.lock): kaminari (= 1.1.1) In Gemfile: activeadmin (~> 1.3) was resolved to 1.3.0, which depends on kaminari (>= 0.15) rails_admin (~> 1.3) was resolved to 1.3.0, which depends on kaminari (< 2.0, >= 0.14) spree (~> 3.5.0) was resolved to 3.5.0, which depends on spree_core (= 3.5.0) was resolved to 3.5.0, which depends on kaminari (~> 1.0.1) Running `bundle update` will rebuild ...

Ngnix still seeing welcome screen after configuration

Ngnix still seeing welcome screen after configuration I have set up and deployed an application using Capistrano, Nginx, Passenger and set up the server and ngnix config file, however, I'm still seeing the Welcome to ngnix welcome screen. Welcome to ngnix Here is what I have in the /etc/nginx/sites-enabled/default file /etc/nginx/sites-enabled/default server { listen 80; listen [::]:80 ipv6only=on; server_name IP; passenger_enabled on; rails_env production; root /home/poladmin/poetry-out-loud-v2/current/public/; # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } I have restarted Ngnix but I'm still not seeing the website. 1 Answer 1 Sounds like Nginx is serving you the content from it's installati...