Manuel Kiessling
2011-Jan-13 13:19 UTC
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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2011-Jan-13 13:50 UTC
Re: Is this thing here a helper or a model, or something else?
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, > > -- > ManuelBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org Sent from my iPhone -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Arailsdemo A.
2011-Jan-13 13:51 UTC
Re: Is this thing here a helper or a model, or something else?
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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Manuel Kiessling
2011-Jan-13 14:00 UTC
Re: Is this thing here a helper or a model, or something else?
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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Arailsdemo A.
2011-Jan-13 18:09 UTC
Re: Is this thing here a helper or a model, or something else?
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.-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2011-Jan-13 18:45 UTC
Re: Is this thing here a helper or a model, or something else?
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.rbOr 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jade Tucker
2011-Jan-13 23:48 UTC
Re: Is this thing here a helper or a model, or something else?
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 method 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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.