is it possible to override built-in Ruby methods? - ruby-on-rails

I am working on a problem where I have to pass an rpsec test. The problem is that the method is using the same name as a built in ruby method .count
given that I cannot change the rspec test, is it possible to override .count to behave differently? if not, is there a better way to get around this?
here is the rspec test I am trying to pass
subject = FinancialSummary.one_day(user: user, currency: :usd)
expect(subject.count(:deposit)).to eq(2)
my code:
class FinancialSummary
def self.one_day(user: user, currency: currency)
one_day_range = Date.today.beginning_of_day..Date.today.end_of_day
find_transaction(user.id, currency).where(created_at: one_day_range)
end
def self.find_transaction(user_id, currency)
Transaction.where(user_id: user_id,
amount_currency: currency.to_s.upcase
)
end
end
output:
[#<Transaction:0x00007f9b39c2e9b8
id: 1,
user_id: 1,
amount_cents: 1,
amount_currency: "USD",
category: "deposit",
created_at: Sat, 10 Mar 2018 18:46:53 UTC +00:00,
updated_at: Sat, 10 Mar 2018 18:46:53 UTC +00:00>,
#<Transaction:0x00007f9b3d0dbc38
id: 2,
user_id: 1,
amount_cents: 2000,
amount_currency: "USD",
category: "deposit",
created_at: Sat, 10 Mar 2018 18:47:43 UTC +00:00,
updated_at: Sat, 10 Mar 2018 18:47:43 UTC +00:00>,
#<Transaction:0x00007f9b3d0b3fa8
id: 7,
user_id: 1,
amount_cents: 1200,
amount_currency: "USD",
category: "withdraw",
created_at: Mon, 05 Mar 2018 02:22:42 UTC +00:00,
updated_at: Tue, 06 Mar 2018 18:48:20 UTC +00:00>]
it is printing out, what I believe to be the correct information, up until the test attempts to count the transactions by their category: 'deposit'. Then I get this error message:
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: deposit: SELECT COUNT(deposit) FROM "transactions" WHERE "transactions"."user_id" = ? AND "transactions"."amount_currency" = ?
EDITED FOR MORE INFO

Some Assumptions Were Made in the Writing of this answer and modifications may be made based on updated specifications
Overriding count is a bad idea because others who view or use your code will have no idea that this is not the count they know and understand.
Instead consider creating a scope for this like
class FinancialSummary < ApplicationRecord
scope :one_day, ->(user:,currency:) { where(user: user, currency: currency) } #clearly already a scope
scope :transaction_type, ->(transaction_type:) { where(category: transaction_type) }
end
then the test becomes
subject = FinancialSummary.one_day(user: user, currency: :usd)
expect(subject.transaction_type(:deposit).count).to eq(2)
SQL now becomes:
SELECT COUNT(*)
FROM
"transactions"
WHERE
"transactions"."user_id" = ?
AND "transactions"."amount_currency" = "usd"
AND "transactions"."category" = "deposit"
Still very understandable and easy to read without the need to destroy the count method we clearly just used.

It's not clear what object the count message is being sent to because I don't know what FinancialSummary.one_day(user: user, currency: :usd) returns, but it seems like you are saying count is a method on whatever it returns, that you can't change. What does FinancialSummary.one_day(user: user, currency: :usd).class return?
Perhaps one solution would be to alias it on that object by adding alias_method :count, :account_count and then in your test calling expect(subject.account_count(:deposit)).to eq(2)
It would be easier if you could post the FinancialSummary#one_day method in your question.

Related

Rails, how to sort a collection keeping a specific value at the end

I have an active record collection of some objects...
#<CoverElement:0x00007f87a4d78718
id: 312,
title: "Title 1",
link: "",
coverable_id: 35001,
coverable_type: "Article",
created_at: Thu, 07 May 2020 16:55:00 CEST +02:00,
updated_at: Thu, 07 May 2020 16:55:00 CEST +02:00,
cover_id: 4,
format: "small",
custom_image: nil,
position: 2,
second_title: nil,
second_title_url: nil>,
#<CoverElement:0x00007f87a4d6fde8
id: 313,
title: "Title 2",
link: "",
coverable_id: 35010,
coverable_type: "Article",
created_at: Thu, 07 May 2020 16:55:00 CEST +02:00,
updated_at: Sun, 22 Nov 2020 19:33:39 CET +01:00,
cover_id: 4,
format: "horizontal",
custom_image: nil,
position: 3,
second_title: nil,
second_title_url: nil>
For the desktop version of the website I use the position as order, but I need another order for the mobile version.
I need to sort by format and "small" must be the last.
Basically formats can be: "large", "vertical", "horizontal" and "small".
I try with
#elements.reorder('format ASC')
and it works but of course it keep the order "horizontal", "large", "vertical" and "small"
Every object with "small" as format must be at the end. Is there a way to do this or I have to write a custom sort method?
you will need another sort method for the mobile version. However if you sort by format, it will do an alphanumeric sort, which will not give you the results you want ("small" is last).
There are a couple of ways to get the order you want:
first way is to recast the format column as an enum, so that the database contains integer values iso strings:
class CoverElement < ActiveRecord::Base
enum format: [ :large, :vertical, :horizontal, :small ]
end
if this is not possible, you can do it with an sql snippet something like this:
order = <<-ORD.squish
CASE
WHEN 'format'='large' THEN 4
WHEN 'format'='vertical' THEN 3
WHEN 'format'='horizontal' THEN 2
WHEN 'format'='small' THEN 1
END
ORD
CoverElement.order(order)

Using Acts As Votable for multiple database tables with Rails 5

I have been using https://github.com/ryanto/acts_as_votable gem as a Save button for Posts. It has been all good so far. 👍
However now I created a separate scaffold (Articles) and wanted to add the same Save button. So users can save Posts and Articles, then view in their profiles.
Now I got problem as some Article records has same id as Post records. Plus how do I even display Saved records now as I dont know what id comes from Article or Post. 🤔
Is there any way to solve this with Acts As Votable Gem?
Thank you! 🙏
The current version (0.12.0) of acts_as_voteable does this out of the box. The Vote model has a column votable_type which can be a reference to multiple models.
#<ActsAsVotable::Vote:0x00007f9f6558a9b0
id: 4,
votable_type: "Post",
votable_id: 1,
voter_type: "User",
voter_id: 2,
vote_flag: true,
vote_scope: "save",
vote_weight: 1,
created_at: Mon, 31 Dec 2018 13:39:34 UTC +00:00,
updated_at: Mon, 31 Dec 2018 13:39:34 UTC +00:00>,
#<ActsAsVotable::Vote:0x00007f9f6558a4d8
id: 5,
votable_type: "Article",
votable_id: 3,
voter_type: "User",
voter_id: 2,
vote_flag: true,
vote_scope: "article",
vote_weight: 1,
created_at: Tue, 01 Jan 2019 15:15:27 UTC +00:00,
updated_at: Tue, 01 Jan 2019 15:15:27 UTC +00:00>
To display saved records you can use a scope like
#user.votes.for_type(Post)
#user.votes.for_type(Article)
I hope this answers your question.

Adding year for all records with update_all

I need to update all records in my model by adding another year before the date saved , seek information and an option is with update_all , I really would help an example with dates update_all database: PostgreSQL
An example:
the saved date is this 15/01/16 after executing the action 01/15/17 and so on all records.
or some other option would be very helpful!
If you use the PostgreSQL database you can use the interval from datetime functions:
$ rails console
=> User.last
=#<User:0x00563c0ed6e0c0
id: 7,
name: "foo",
email: "foo#test.ru",
created_at: Tue, 15 Mar 2016 19:54:52 MSK +03:00,
^^^^
.......
=> User.update_all("created_at = created_at + '1 year'::interval")
=> #<User:0x00563c0c9e4f78
id: 7,
name: "foo",
email: "foo#test.ru",
created_at: Wed, 15 Mar 2017 19:54:52 MSK +03:00,
^^^^
........
If MySQL database you can use the DATE_ADD function.
All of this should work if the column have a right type.

Comparing ranges of datetimes in Ruby on Rails against one-another

Right now I'm trying to see if a certain show's start and end times overlap another show that's currently recording => true where 'show' is the TV show the user wants to record.
def self.record_show
shows = Box.first.shows.where(:recording => true).flatten
show_start_and_end_times = shows.collect {|x| x.start_time..x.end_time}
current_show_time = show.start_time..show.end_time
overlap = show_start_and_end_times.select {|c| current_show_time.overlaps?(c)}
if overlap.present?
nil
else
show.update_attributes(:recording => true)
show.save
end
end
It runs the method, but I'm having difficulty figuring out how to get it so that it finds the actual currently recording show that's causing the overlap. So for example, let's say in 'shows' I currently have two shows:
[#<Show id: 181, box_id: 78, title: "The Fox", channel: 22, single_recording: true, created_at: "2014-08-12 19:55:49", updated_at: "2014-08-12 20:09:24", start_time: "2014-08-12 19:55:49", end_time: "2014-08-12 20:25:49", recording: true>, #<Show id: 186, box_id: 78, title: "Funniest Home Videos", channel: 45, single_recording: true, created_at: "2014-08-12 19:55:49", updated_at: "2014-08-12 20:09:27", start_time: "2014-08-12 23:20:49", end_time: "2014-08-13 00:20:49", recording: true>]
In show_start_and_end_times I have:
[Tue, 12 Aug 2014 19:55:49 UTC +00:00..Tue, 12 Aug 2014 20:25:49 UTC +00:00, Tue, 12 Aug 2014 23:20:49 UTC +00:00..Wed, 13 Aug 2014 00:20:49 UTC +00:00]
In current_show_time I have:
Tue, 12 Aug 2014 19:55:49 UTC +00:00..Tue, 12 Aug 2014 20:55:49 UTC +00:00
Which means that in overlap I have the start_time..end_time of the first show_start_and_end_times show, which is the one that is causing the overlap:
[Tue, 12 Aug 2014 19:55:49 UTC +00:00..Tue, 12 Aug 2014 20:25:49 UTC +00:00]
I tried comparing the two times against one-another:
(shows.first.start_time..shows.first.end_time) == (overlap.first)
Which gives me false, even though the times are exactly the same. How can I compare the overlap time against the shows list to figure out which show is causing the overlap?
You'll wand to check out:
http://guides.rubyonrails.org/active_record_querying.html
Using info from that you might be able to do something like:
def self.record_show
overlapping_shows = Box.first.shows.where(recording: true).where("start_time <= :show_end AND end_time >= :show_start", {show_start: show.start_time, show_end: show.end_time}).flatten
if overlapping_shows.present?
nil
else
show.update_attributes(:recording => true)
show.save
end
end
This is a pretty common problem, and I'd recommend checking out this SO question for general algorithm advice on how to do the overlap checking:
Determine Whether Two Date Ranges Overlap
UPDATE:
I would change it to something like this:
shows = Box.first.shows.where(:recording => true).flatten
overlapping_show = nil
current_show_time = show.start_time..show.end_time
shows.each do |s|
if current_show_time.overlaps?(s.start_time..s.end_time)}
overlapping_show = s
break # you could alternatively return an array of overlapping
# if you anticipate more than 1 will overlap
end
end
if overlap.present? #...

Most proper way to use inherited classes with shared relations?

I have the TestVisual class that is inherited by the Game class :
class TestVisual < Game
include MongoMapper::Document
end
class Game
include MongoMapper::Document
belongs_to :maestra
key :incorrect, Integer
key :correct, Integer
key :time_to_complete, Integer
key :maestra_id, ObjectId
timestamps!
end
As you can see it belongs to Maestra.
So I can do Maestra.first.games which returns []
But I can not to Maestra.first.test_visuals because it returns undefined method test_visuals
Since I'm working specifically with TestVisuals, that is ideally what I would like to pull, but still have it share the attributes of its parent Game class.
Is this possible with Mongo. If it isn't or if it isn't necessary, is there any other better way to reach the TestVisual object from Maestra and still have it inherit Game ?
Single Collection Inheritance (SCI) in MongoMapper auto-generates selection,
ex., the following produce the same results.
p Game.where(_type: 'TestVisual').all
p TestVisual.all
See also mongomapper/lib/mongo_mapper/plugins/sci.rb - MongoMapper::Plugins::Sci::ClassMethods#query
However, MongoMapper does not auto-generate associations for subclasses based on the base class' associations,
and I don't think that this should be expected.
Note that SCI places subclasses and base classes in the same MongoDB collection.
If this is not what you want, you should consider other mechanisms for modularity.
You can define the following method yourself for an association accessor method, perhaps this is sufficient for your purposes?
For other association methods like append or push, the parent methods are probably workable.
class Maestra
include MongoMapper::Document
key :name, String
many :games
def test_visuals
games.where(_type: 'TestVisual')
end
end
test/unit/test_visual_test.rb
require 'test_helper'
def ppp(obj)
puts obj.inspect.gsub(/, ([^#])/, ",\n\t\\1").gsub(/, #/, ",\n #")
end
class TestVisualTest < ActiveSupport::TestCase
def setup
Maestra.delete_all
Game.delete_all
end
test "inheritance" do
maestra = Maestra.create(name: 'Fiona')
maestra.games << Game.create(incorrect: 1, correct: 9, time_to_complete: 60)
maestra.games << TestVisual.create(incorrect: 2, correct: 8, time_to_complete: 61)
ppp maestra.games.to_a
ppp maestra.test_visuals.to_a
end
end
output
Run options: --name=test_inheritance
# Running tests:
[#<Game _id: BSON::ObjectId('4ff7029a7f11ba6e43000002'),
_type: "Game",
correct: 9,
created_at: Fri,
06 Jul 2012 15:22:02 UTC +00:00,
incorrect: 1,
maestra_id: BSON::ObjectId('4ff7029a7f11ba6e43000001'),
time_to_complete: 60,
updated_at: Fri,
06 Jul 2012 15:22:02 UTC +00:00>,
#<TestVisual _id: BSON::ObjectId('4ff7029a7f11ba6e43000003'),
_type: "TestVisual",
correct: 8,
created_at: Fri,
06 Jul 2012 15:22:02 UTC +00:00,
incorrect: 2,
maestra_id: BSON::ObjectId('4ff7029a7f11ba6e43000001'),
time_to_complete: 61,
updated_at: Fri,
06 Jul 2012 15:22:02 UTC +00:00>]
[#<TestVisual _id: BSON::ObjectId('4ff7029a7f11ba6e43000003'),
_type: "TestVisual",
correct: 8,
created_at: Fri,
06 Jul 2012 15:22:02 UTC +00:00,
incorrect: 2,
maestra_id: BSON::ObjectId('4ff7029a7f11ba6e43000001'),
time_to_complete: 61,
updated_at: Fri,
06 Jul 2012 15:22:02 UTC +00:00>]
.
Finished tests in 0.026661s, 37.5080 tests/s, 0.0000 assertions/s.
1 tests, 0 assertions, 0 failures, 0 errors, 0 skips

Resources