您将如何将业务时间存储在Rails应用的db / model中? [英] How would you store a business's hours in the db/model of a Rails app?

查看:106
本文介绍了您将如何将业务时间存储在Rails应用的db / model中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Rails应用程序,用于存储业务的开放和关闭时间。最初,我只想使用一种文本数据类型,并使其为自由格式:

I'm creating a Rails app that will store the opening and closing hours for a business. Originally, I thought of simply using a text data type and letting it be free-form:

"Monday to Friday 9am to 5pm
Saturday 11am to 4pm
Closed Sundays"

但是,我需要检查当前日期的时间&时间并在视图中显示打开或关闭。类似于:

But, requirements have changed and I need to check the hours against the current date & time and display an "Open" or "Closed" in the view. Something like:

class Business < ActiveRecord::Base

  def open?
    # Something like ... 
    Time.now > open_time && Time.now < close_time
  end

end

那么最好的在一周的每一天的存储时间方面可以解决这个问题?业务是否只需要open_block(或任何)有开放和关闭的时间?我应该把这一天当作一个字符串来存储吗?

So what would be the best way to tackle this in terms of storing the hours for each day of the week? Should the Business simply has_many :open_blocks (or whatever) that have open and close times? Should I just store the day as a string?

推荐答案

我正在为客户端设置目录列表,我们希望给予用户更多的灵活性。所以:让用户设置天数:

I'm currently setting up a directory listing for a client and we want give the user more flexibility. So: Let the user set up blocks for days:

我们有一个一天(整数1-7),打开关闭(时间),一些商店或景点有夏季和冬季开放时间,或者休假。根据 schema.org ,您必须添加一个valid_from(datetime)和valid_through(datetime)。

We have a day (integer 1-7), opens (time), closes (time) and some shops or sights have summer and winter opening times, or they make vacations. According to schema.org you have to add a valid_from (datetime) and a valid_through (datetime).

使用此设置,用户可以创建他想要的任何内容:

With this setup the user can create whatever he wants:

# migration
class CreateOpeningHours < ActiveRecord::Migration
  def change
    create_table :opening_hours do |t|
      t.integer :entry_id # your model reference
      t.integer :day
      t.time :closes
      t.time :opens
      t.datetime :valid_from
      t.datetime :valid_through
    end
  end
end

模型示例:

class OpeningHour < ActiveRecord::Base

  belongs_to :entry

  validates_presence_of :day, :closes, :opens, :entry_id
  validates_inclusion_of :day, :in => 1..7
  validate :opens_before_closes 
  validate :valid_from_before_valid_through 

  # sample validation for better user feedback
  validates_uniqueness_of :opens, scope: [:entry_id, :day]
  validates_uniqueness_of :closes, scope: [:entry_id, :day]

  protected
  def opens_before_closes
    errors.add(:closes, I18n.t('errors.opens_before_closes')) if opens && closes && opens >= closes
  end

  def valid_from_before_valid_through
    errors.add(:valid_through, I18n.t('errors.valid_from_before_valid_through')) if valid_from && valid_through && valid_from >= valid_through
  end

end

设置你可以轻松创建一个is_open?方法在你的模型。目前我没有设置is_open?方法,但如果有人需要,给我一个打击!我想我会在接下来的几天完成。

With that setup you can create easily a is_open? method in your model. Currently I did not setup the is_open? method, but if somebody needs, give me a hit! I think I will finish it in the next days.

这篇关于您将如何将业务时间存储在Rails应用的db / model中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆