6 steps to create data entry program
Step 1: Create model
Model defines table (collection) fields, indexes, validations and document callbacks in single source file.
(app/models/note.rb)
########################################################################### # Note model definition. ########################################################################### 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(body, :tags=>[]).to_s text.gsub!(/\,|\.|\)|\(|\:|\;|\?/,'') text.gsub!(' ',' ') text.gsub!('>',' ') text.gsub!('<',' ') text.squish! self.search = (title + text).downcase end end
Step 2: Use rails generator to generate form
rails generate new_drg_form note
Step 3: Edit generated form
(app/forms/note.yml)
table: note index: filter: title, search as text_field actions: 1: type: new caption: New note 2: filter result_set: filter: current_users_documents actions: 1: edit columns: 10: name: time_started width: 10% format: '%d.%m.%Y' 20: name: title name: 25% 30: name: duration eval: dc_name4_value form: tabs: tab1: 10: name: user_id type: readonly eval: dc_name4_id,dc_user,name default: eval: 'session[:user_id]' 20: name: title type: text_field size: 50 30: name: time_started type: datetime_picker options: step: 15 40: name: duration type: select tab2: 10: name: body type: html_field options: "height: 500"
Step 4: Define data entry field labels and help text
Field labels and help text are defined in standard Rails i18n translation text files. At the bottom of the generated form you will find template for field labels with field names. Complete label names and help text and save (cut+paste) it into configuration/locals folder.
(config/locale/portal_en.yml)
en: 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" tab1: Main tab2: Description 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
Step 5: Create form's control file (if needed)
Control files define callbacks which are called before or after controller actions. In our case we create two callbacks.
dc_new_record is called after new empty record is created for editing. We set currently logged user's id into user_id field.
notes_filter (defined in forms result_set:filter) will provide that user sees only his own notes.
(controls/note_control.rb)
###################################################################### # Control file for Note form ###################################################################### module NoteControl ###################################################################### # Set form filter so only notes belonging to current user are selected ###################################################################### def current_users_documents user_filter_options(Note).and(user_id: session[:user_id]) end end
Step 6: Include form in Notes application menu
(views/notes/_menu.erb)
<ul class="dc-action-menu app-menu"> <li class="dc-link dc-animate"> <%= dc_link_to 'Notes', 'book', { controller: :cmsedit, table: 'note', form_name: 'note' }, target: 'iframe_edit' %> </li> </ul> <%= dc_iframe_edit('note') %>
Test run the Notes application
The Note form is also embedded below. Test it if you want it.
You will notice, that you are not authorized to save any data. You have to logon as user test (password test) to add or edit notes.