Notes model
We started with design and menu. Let us now introduce to document model which will be used for saving data.
class Note include Mongoid::Document include Mongoid::Timestamps field :title, type: String field :body, type: String field :time_begin, type: DateTime field :duration, type: Integer field :search, type: String field :user_id, type: BSON::ObjectId index user_id: 1 validates :title, presence: true validates :time_begin, presence: true validates :duration, presence: true before_save :fill_search_field ############################################################################# # Before save remove all html tags from body field and save data into search field. ############################################################################# def fill_search_field text = ActionView::Base.full_sanitizer.sanitize(self.body, :tags=>[]).to_s text.gsub!(/\,|\.|\)|\(|\:|\;|\?/,'') text.gsub!(' ',' ') text.gsub!('>',' ') text.gsub!('<',' ') text.squish! self.search = UnicodeUtils.downcase(self.title + text) end end
Note model has title and body field. It can be used just for saving notes but can also be used to track work so it has time_begin and duration fields. It also has user_id field so we know who owns the document. There is search field which holds title and body striped of all html tags and will be used for full text searching. fill_search_field method will take care of filling search field.
Before we create DRG form it is good practice to define field labels and help text for editing fields on form. Ruby on Rails already provides mechanism for setting form fields labels. It is called i18n or localization. Localized field labels can be provided in yaml formatted files located in config/locales directory. Lets name our localization file models_en.yml.
helpers:
label:
note:
tabletitle: Notes
choices4_duration: "10 min:10,15 min:15,20 min:20,30 min:30,45 min:45,1 hour:60,1 hour 30 min:90,2 hours:120,2 hours 30 min:150,3 hours:180,4 hours:240,5 hours:300,6 hours:360,7 hours:420,8 hours:480"
title: Title
body: Description
time_start: Start time
duration: Duration
search: Search
user_id: Created by
help:
note:
title: Short title
body: Description of event or note
time_start: Time or date when note is created or event started
duration: Duration of event
search: Data used for searching data
user_id: This note was created by
First part uses default Rails translation keywords.
en: helpers: label: model_name: fields:
Second part is for displaying help text when user moves cursor over label of input field.
en: helpers: help: model_name: fields:
All we need now is to provide DRG CMS data entry form. We will do this in next chapter.