Project

General

Profile

Customization » History » Version 5

Phil Hodgson, 05/20/2014 07:37 PM

1 1 Phil Hodgson
h1. Customization
2
3
h2. Overriding Default Views, Partials, Templates, etc.
4
5 2 Phil Hodgson
It is possible to override any Rails view in the application by mimicking the directory structure in @app/views@ but under another folder @site_specific/app/views@. For example, to use your own version of @_dashboard.html.erb@ in @app/views/pages@ you would put it in @site_specific/app/views/pages@.
6
7
You can override the @#{Rails.root}/site_specific@ folder itself with the environment variable TAPESTRY_OVERRIDE_PATH, so that the folder can be left entirely outside of the Tapestry code base.
8
9
It is important to understand that including this folder, any subfolders, and all files is _optional_. If you do not wish to override a particular view, leave it out of the override folder.
10 3 Phil Hodgson
11
h3. Caveat
12
13
_This statement to be followed up after more investigation._
14
15
It is my impression that when using multiple paths that the technique of using @explicitly_unloadable_constants@ for having files reload without restarting the server will not work properly. This could mean that while developing these site-specific files that the server has to be restarted after each change.
16
17
h3. Overriding @lib@ Files
18
19
The same logic works for files in the override path under the @lib@ subfolder, i.e. either @#{Rails.root}/site_specific/lib@ or @#{ENV['TAPESTRY_OVERRIDE_PATH']}/lib@.
20
21
h2. Adding Custom Questions to the "Participation Consent" Form
22
23
Currently this text and form are found in @views/participation_consents/show.html.erb@. This currently saves the user's responses in the InformedConsentResponse model. There is in this model a field called "other_answers" that is a serialized Hash where any number of "dynamically defined" answers can be saved with keys of your choosing. To accomplish this you have to add form inputs that end up with a @name@ attribute that looks like (e.g. to record "age"): @other_answers[age]@ and it will be recorded in the "other_answers" Hash in the model under the :age key.
24
25
There is a view helper for creating radio boxes for the participation concept form using this "other_answers" field. Example:
26
27
<pre>
28
  <div class="consent-form-question">
29
    <p>
30
      Would you judge yourself to be sane?
31
      <%= radio_answers( 'sanity', [['0', 'No'],
32
                                    ['1', 'Sometimes'],
33
                                    ['2', 'Yes']] ) %>
34
    </p>
35
  </div>
36
</pre>
37
38 4 Phil Hodgson
There are also helpers for text areas (@text_area_answer@) and standard input texts (@text_field_answer@). For example:
39 3 Phil Hodgson
40 4 Phil Hodgson
<pre>
41
  <div class="consent-form-question">
42
    <p>
43
      If sane only sometimes, please explain when and for what reason this occurs:
44
    </p>
45
    <p>
46
      <%= text_area_answer 'reason_sometimes_sane', { :cols => 60, :rows => 3 } %>
47
    </p>
48
  </div>
49
</pre>
50
51
52 3 Phil Hodgson
h3. Adding Custom Validation of Your Custom Question
53 1 Phil Hodgson
54 4 Phil Hodgson
This is done by adding a site-specific validations file in the @lib@ "override" folder, in the @lib/site_specific/validations.rb@ module. First place the following code in the file:
55 3 Phil Hodgson
56 1 Phil Hodgson
<pre>
57 4 Phil Hodgson
module SiteSpecific
58
  module Validations
59
    extend ActiveSupport::Concern
60
61
    # *Do not remove this +included+ block!* It is what works the magic.
62
    included do
63
      method_name = "#{self.name.to_s.underscore}_validations"
64
      validate method_name if method_defined? method_name
65
    end
66
67
  end
68
end
69
</pre>
70
71
Then after this insert a method called @informed_consent_response_validations@ (after the name of the model relevant to the Participation Consent form). You can check @other_answers@ and add errors in the standard ActiveRecord way. For example:
72
73
<pre>
74 1 Phil Hodgson
    def informed_consent_response_validations
75 4 Phil Hodgson
      case self.other_answers[:sanity]
76
      when '2'
77 1 Phil Hodgson
        errors.add( :other_answers, :sanity_not_permitted)
78 4 Phil Hodgson
      when '1'
79
        if self.other_answers[:reason_sometimes_sane].blank?
80
          errors.add( :other_answers, :explain_occasional_sanity )
81
        end
82 3 Phil Hodgson
      end
83 1 Phil Hodgson
    end
84 3 Phil Hodgson
</pre>
85
86 4 Phil Hodgson
The messages should be placed in your locale file under:
87 3 Phil Hodgson
88
<pre>
89
en:
90
  activerecord:
91
    errors:
92 1 Phil Hodgson
      models:
93
        informed_consent_response:
94
          attributes:
95
            other_answers:
96 4 Phil Hodgson
              sanity_not_permitted:
97
                You are not permitted to be sane.
98
              explain_occasional_sanity:
99
                Please explain when and why you are sometimes sane.
100 3 Phil Hodgson
</pre>
101 4 Phil Hodgson
102
See the documentation on [[Internationalization]] for where to put this.
103 5 Phil Hodgson
104
h4. Automatic Reloading of the Validations Override During Development
105
106
As shown in the @development.rb.example@ file, you can add the following line to your @development.rb@ to have validation override changes automatically reload without having to restart your Rails server:
107
108
<pre>
109
ActiveSupport::Dependencies.explicitly_unloadable_constants << 'SiteSpecific::Validations'
110
</pre>