Last active
October 26, 2016 22:42
-
-
Save bhserna/55ffa53bdae31eae5282ef9faa06d90d to your computer and use it in GitHub Desktop.
Rails no es mi app…. pero ¿Que tanto debería delegar al modelo?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
def self.last_registered(number) | |
order(id: :desc).limit(number) | |
end | |
end | |
class Admin::UsersExportsController < Admin::BaseController | |
def create | |
export = Users.export_users(User, params[:users_export], Date.current) | |
# other stuff... | |
end | |
end | |
module Users | |
def self.export_users(store, params, current_date) | |
# stuff.. | |
records = store.last_registered(form.number_of_users) | |
# stuff.. | |
end | |
end | |
module Users | |
RSpec.describe "Export users" do | |
describe "generates a list of users" do | |
it "with the given number of last registered users" do | |
store = store_with (1..100).map { |id| user_with(id: id) } | |
export = export_users(store, "number_of_users" => 10) | |
expect(export.users_list.count).to eq 10 | |
expect(export.users_list.first.id).to eq 100 | |
end | |
end | |
def store_with(users) | |
FakeUsersStore.new(users) | |
end | |
def user_with(attrs) | |
FakeUser.new(attrs) | |
end | |
def export_users(store, params, current_date = Date.current) | |
Users.export_users(store, params, current_date) | |
end | |
class FakeUsersStore | |
def initialize(records) | |
@records = records | |
end | |
def last_registered(number) | |
records.reverse.first(number) | |
end | |
private | |
attr_reader :records | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment