0

I'm working on a rails application with no models. I have a class in lib, FakeMaker, that builds up a bunch of fake entities for display purposed.

I want to test out deletion functionality but the problem is that my fake data set re-initializes every time I hit the controller.

I'd like to run the data test creator only once so that I only have one set of fake data.

I have tried using ||=, before_filter, class methods in FakeMaker, sessions storage but they all seem to have the issue of reinitializing the data set everytime the controller is hit.

Controller code:

class HomeController < ApplicationController
  include FakeMaker

  before_filter :set_up_fake_data

  def index
    @workstations = @data[:workstations]
    @data_sources = @data[:data_sources]
  end

  private

  def set_fake_data
    @data ||= session[:fake_data]
  end

  def initialize_data
    session[:fake_data] = set_up_fake_data
  end
end  

FakeMaker in lib:

module FakeMaker
  include ActionView::Helpers::NumberHelper

  SOURCE_CIDNE = "CIDNE"
  SOURCE_DCGS = "DCGS"
  TYPES_CIDNE = Faker::Lorem.words(num = 10)
  TYPES_DCGS = Faker::Lorem.words(num = 4)

  def set_up_fake_data
    @data ||= { workstations: fake_maker("workstation", 8), data_sources: fake_maker("data_source", 2) }
  end

  def fake_maker(type, count)
    fake = []
    case type
    when "report"
      count.times { fake << fake_report }
    when "workstation"
      count.times { fake << fake_workstation }
    when "data_source"
      fake = fake_data_source
    end
    fake
  end

  def fake_report
    report = { source: [SOURCE_CIDNE, SOURCE_DCGS].sample,
               count: number_with_delimiter(Faker::Number.number(5), :delimiter => ',') }
    report[:type] = report[:source] == SOURCE_CIDNE ? TYPES_CIDNE.sample : TYPES_DCGS.sample.capitalize
    report
  end

  def fake_workstation
    { name: Faker::Lorem.word,
      downloaded: number_with_delimiter(Faker::Number.number(3), :delimiter => ','),
      available: number_with_delimiter(Faker::Number.number(5), :delimiter => ','),
      last_connect: fake_time,
      queueJSON: fake_queueJSON,
      historyJSON: fake_historyJSON }
  end

  def fake_data_source
    data_sources = []
    ["CIDNE", "DCGS"].each do |source|
      data_sources << { type: source,
                        name: Faker::Internet.url,
                        status: ["UP", "DOWN"].sample }
    end
    data_sources
  end


  def fake_historyJSON
    history = []
    12.times { history << fake_history }
    history
  end

  def fake_queueJSON
    queue = []
    35.times { queue << fake_queue }
    queue
  end

  def fake_history
    { sentTimestamp: fake_time,
      reportID: Faker::Number.number(5)}
  end

  def fake_queue
    { priority: Faker::Number.number(3),
      queuedTimestamp: fake_time,
      reportID: Faker::Number.number(5)}
  end

  def fake_time
    Random.rand(10.weeks).ago.strftime("%Y-%m-%d %H:%M:%S")
  end


end
steve_gallagher
  • 3,778
  • 8
  • 33
  • 51
  • Have a look at [this page from Objects On Rails](http://objectsonrails.com/#ID-17445728-2450-47ad-b6da-eb2194b503d3) which explains how to initialize an app-wide persistence object. – Zach Kemp Sep 24 '13 at 16:18
  • you can generate the fake data on the db/seed.rm file and you can generate it by doing `rake db:seed` when you need to create more fake data – MZaragoza Sep 24 '13 at 16:28
  • @ZachKemp Thanks. That worked. My only concern is that when I go to delete some items it might complain since it's supposed to be a constant. – steve_gallagher Sep 24 '13 at 16:38
  • @MoisesZaragoza Thanks for that. I didn't know you could use db/seed.rb for non-persistent data. I don't have a database at all with this application. – steve_gallagher Sep 24 '13 at 16:39
  • @steve_gallagher - You can modify the object the constant refers to without raising complaints (for example, you can add and remove objects from an array assigned to a constant, and even use the bang methods like `map!`, etc). You'll only run into warnings if you try to assign it another object. – Zach Kemp Sep 24 '13 at 18:29
  • @ZachKemp Your right. My concerns turned out to be a total non-issue. It's working great. Thanks. – steve_gallagher Sep 24 '13 at 19:53

0 Answers0