Is this thing here a helper or a model, or something else?

Hello,

for my Rails application I’m going to need a class that does timestamp
computations, e.g. “give me a list of all 5-minute-blocks for a certain
time range”.

Example:

.set_start_time(‘2011-01-13 12:00:00’)
.set_end_time(‘2011-01-13 12:15:00’)
.get_time_blocks(5)

-> results in

[‘2011-01-13 12:00:00’,
2011-01-13 12:05:00,
2011-01-13 12:10:00,
2011-01-13 12:15:00]

I’m having problems to decide what kind of class this should be in a
Rails application? It really is just a helper and not part of the
business logic, thus it could be a helper, but as far as I understand
helpers are for controllers and views, and this class is going to be
used by models. So, maybe it’s a model?

Thanks in advance,


Manuel

Manuel Kiessling wrote in post #974606:

Hello,

for my Rails application I’m going to need a class that does timestamp
computations, e.g. “give me a list of all 5-minute-blocks for a certain
time range”.

Example:

.set_start_time(‘2011-01-13 12:00:00’)
.set_end_time(‘2011-01-13 12:15:00’)
.get_time_blocks(5)

→ results in

[‘2011-01-13 12:00:00’,
2011-01-13 12:05:00,
2011-01-13 12:10:00,
2011-01-13 12:15:00]

I’m having problems to decide what kind of class this should be in a
Rails application? It really is just a helper and not part of the
business logic, thus it could be a helper, but as far as I understand
helpers are for controllers and views, and this class is going to be
used by models. So, maybe it’s a model?

I think it’s a model, or simply a utility class that goes in lib.

Thanks in advance,


Manuel

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Sent from my iPhone

These methods are not generating any HTML, so they should not be in a
view helper. Like you said, they are not needed for controller function,
so they shouldn’t be in the ApplicationController. So I would put them
in your model.

You can put this logic in a module in your lib/ directory. This way if
you need the code in different models, then you can just require/include
it.

module ChunkyTime

def set_start_time …
def set_end_time …
def get_time_blocks …

end

class MyModel
include ChunkyTime

end

Thanks guys, just learnt another “Ruby way” of doing stuff, you helped a
lot!

Arailsdemo: “chunk” is a much better fitting name for what I’m planning
to implement, thanks for this.

If you haven’t worked with the lib/ directory before, make sure you have

    config.autoload_paths += %W(#{config.root}/lib)

in config/application.rb

Also, if you ever want to make yourself a gem, you could do something
like

lib/acts_as_chunky_time

module ActsAsChunkyTime

def self.included(class_thats_including_my_module)
class_thats_including_my_module.extend ClassMethods
end

module ClassMethods
def acts_as_chunky_time
include ActsAsChunkyTime::InstanceMethods
end
end

module InstanceMethods
def set_start_time …
def set_end_time …
def get_time_blocks …
end
end

ActiveRecord::Base.send(:include, ActsAsChunkyTime)

my_model.rb

class MyModel < AR::Base
acts_as_chunky_time
end

Manuel Kiessling wrote in post #974620:

Thanks guys, just learnt another “Ruby way” of doing stuff, you helped a
lot!

Arailsdemo: “chunk” is a much better fitting name for what I’m planning
to implement, thanks for this.

I was thinking about this problem as it is very interesting to myself
being a relatively new ruby/rails programmer. How about opening and
extending the Range class to handle stepping through DateTimes. Not
sure how to do this, but heres the idea:

span = DateTime.parse(“2011-01-13
12:00:00”)…DateTime.parse(“2011-01-13 12:15:00”)

span.step(5.minutes) { |s| p s } # the 5.minutes is not right though

perhaps a custom function then?

span.step_every_five_minutes { |s| p s }

This code to create the behavior would be placed in a patch file in

lib/
lib/range_patch.rb

class Range

end

Just brainstorming here : )

Jade

Arailsdemo A. wrote in post #974689:

If you haven’t worked with the lib/ directory before, make sure you have

    config.autoload_paths += %W(#{config.root}/lib)

in config/application.rb

Or not. You can also require your lib files explicitly if you’d rather,
though autoloading is very convenient.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]