Customization » History » Version 14
Phil Hodgson, 06/20/2014 04:31 PM
1 | 1 | Phil Hodgson | h1. Customization |
---|---|---|---|
2 | |||
3 | 13 | Phil Hodgson | h2. Configuration File Options |
4 | |||
5 | The settings of the @config/config.yml@ file can be set to override the settings of the @config/config.defaults.yml@ file. |
||
6 | |||
7 | h3. email_enrollment_submitted_notification |
||
8 | |||
9 | Set this variable to @true@ to send the email in @app/views/user_mailer/enrollment_submitted_notification.text.erb@. See below (in the section on Overriding Default Views) for instructions on how to safely alter the contents of this email. |
||
10 | |||
11 | 6 | Phil Hodgson | h2. Enabling and Disabling "Sections" of Tapestry |
12 | |||
13 | Tapestry has been divided into a handful of logical "sections". To date, they are: |
||
14 | |||
15 | * Section::SIGNUP |
||
16 | 14 | Phil Hodgson | |
17 | bq. Removing this Section will make it impossible to create a new login account. |
||
18 | |||
19 | * Section::ENROLL |
||
20 | |||
21 | bq. Removing this Section will display the contents of the file @app/views/pages/_enrollment_disabled_notice.html.erb@ instead of allowing the user to go through the enrollment process. |
||
22 | |||
23 | 6 | Phil Hodgson | * Section::PUBLIC_DATA |
24 | 14 | Phil Hodgson | |
25 | bq. Removing this Section will hide and disallow access to the various pages normally seen in the "Public Data" application menu. |
||
26 | |||
27 | 8 | Phil Hodgson | * Section::PUBLIC_PROFILE |
28 | 14 | Phil Hodgson | |
29 | bq. Removing this Section will make all access to Public Profiles, by anyone disallowed, and links to these hidden. |
||
30 | |||
31 | 6 | Phil Hodgson | * Section::GOOGLE_SURVEYS |
32 | 14 | Phil Hodgson | |
33 | bq. Removing this Section makes all mention of and access to the Google Surveys disappear. |
||
34 | |||
35 | 6 | Phil Hodgson | * Section::SAMPLES |
36 | 14 | Phil Hodgson | |
37 | bq. Removing this Section specifically hides mention of and disallows access to the "Samples" part of Public Data. |
||
38 | |||
39 | 9 | Phil Hodgson | * Section::CCR |
40 | 14 | Phil Hodgson | |
41 | bq. Removing this Section specifically hides mention of and disallows access to the functionality for uploading CCR XML files. |
||
42 | 6 | Phil Hodgson | |
43 | In your @config.yml@ file you can specify an array of these and assign it to the @enabled_sections@ config parameter. An example is in the @config.defaults.yml@, where only @Section::SIGNUP@ is enabled by default. |
||
44 | |||
45 | Furthermore, in your overridden views, partials, etc. (see section below) you can use embedded Ruby to access which sections are enabled with the @include_section?@ helper method. Search in the source code for examples. |
||
46 | |||
47 | 12 | Phil Hodgson | h2. Overriding Default Views, Partials, Templates, eMails etc. |
48 | 2 | Phil Hodgson | |
49 | 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@. |
||
50 | 1 | Phil Hodgson | |
51 | 6 | Phil Hodgson | 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. |
52 | 2 | Phil Hodgson | |
53 | 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. |
||
54 | 3 | Phil Hodgson | |
55 | h3. Caveat |
||
56 | |||
57 | _This statement to be followed up after more investigation._ |
||
58 | |||
59 | 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. |
||
60 | |||
61 | h3. Overriding @lib@ Files |
||
62 | |||
63 | 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@. |
||
64 | |||
65 | h2. Adding Custom Questions to the "Participation Consent" Form |
||
66 | |||
67 | 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. |
||
68 | |||
69 | There is a view helper for creating radio boxes for the participation concept form using this "other_answers" field. Example: |
||
70 | |||
71 | <pre> |
||
72 | <div class="consent-form-question"> |
||
73 | <p> |
||
74 | Would you judge yourself to be sane? |
||
75 | <%= radio_answers( 'sanity', [['0', 'No'], |
||
76 | ['1', 'Sometimes'], |
||
77 | ['2', 'Yes']] ) %> |
||
78 | </p> |
||
79 | </div> |
||
80 | </pre> |
||
81 | |||
82 | 4 | Phil Hodgson | There are also helpers for text areas (@text_area_answer@) and standard input texts (@text_field_answer@). For example: |
83 | 3 | Phil Hodgson | |
84 | 4 | Phil Hodgson | <pre> |
85 | <div class="consent-form-question"> |
||
86 | <p> |
||
87 | If sane only sometimes, please explain when and for what reason this occurs: |
||
88 | </p> |
||
89 | <p> |
||
90 | <%= text_area_answer 'reason_sometimes_sane', { :cols => 60, :rows => 3 } %> |
||
91 | </p> |
||
92 | </div> |
||
93 | </pre> |
||
94 | |||
95 | |||
96 | 3 | Phil Hodgson | h3. Adding Custom Validation of Your Custom Question |
97 | 1 | Phil Hodgson | |
98 | 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: |
99 | 3 | Phil Hodgson | |
100 | 1 | Phil Hodgson | <pre> |
101 | 4 | Phil Hodgson | module SiteSpecific |
102 | module Validations |
||
103 | extend ActiveSupport::Concern |
||
104 | |||
105 | # *Do not remove this +included+ block!* It is what works the magic. |
||
106 | included do |
||
107 | method_name = "#{self.name.to_s.underscore}_validations" |
||
108 | validate method_name if method_defined? method_name |
||
109 | end |
||
110 | |||
111 | end |
||
112 | end |
||
113 | </pre> |
||
114 | |||
115 | 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: |
||
116 | |||
117 | <pre> |
||
118 | 1 | Phil Hodgson | def informed_consent_response_validations |
119 | 4 | Phil Hodgson | case self.other_answers[:sanity] |
120 | when '2' |
||
121 | 1 | Phil Hodgson | errors.add( :other_answers, :sanity_not_permitted) |
122 | 4 | Phil Hodgson | when '1' |
123 | if self.other_answers[:reason_sometimes_sane].blank? |
||
124 | errors.add( :other_answers, :explain_occasional_sanity ) |
||
125 | end |
||
126 | 3 | Phil Hodgson | end |
127 | 1 | Phil Hodgson | end |
128 | 3 | Phil Hodgson | </pre> |
129 | |||
130 | 4 | Phil Hodgson | The messages should be placed in your locale file under: |
131 | 3 | Phil Hodgson | |
132 | <pre> |
||
133 | en: |
||
134 | activerecord: |
||
135 | errors: |
||
136 | 1 | Phil Hodgson | models: |
137 | informed_consent_response: |
||
138 | attributes: |
||
139 | other_answers: |
||
140 | 4 | Phil Hodgson | sanity_not_permitted: |
141 | You are not permitted to be sane. |
||
142 | explain_occasional_sanity: |
||
143 | Please explain when and why you are sometimes sane. |
||
144 | 3 | Phil Hodgson | </pre> |
145 | 4 | Phil Hodgson | |
146 | 1 | Phil Hodgson | See the documentation on [[Internationalization]] for where to put this. |
147 | 6 | Phil Hodgson | |
148 | h2. Overriding the Validations on Any Model |
||
149 | |||
150 | The above section explaining how to override validations for the @InformedConsentResponse@ model can serve as an example for the general case. There is only one change required to the Tapestry source code base. In general, this is discouraged, so you should consider contacting the Tapestry development team and letting them know that you've found a need to override validation on a particular model, but the change is slight and easy to deal with in future merges. Basically, you must insert, _after any model validations_, the following line: |
||
151 | |||
152 | <pre> |
||
153 | include SiteSpecific::Validations rescue {} |
||
154 | </pre> |
||
155 | |||
156 | If you do not insert this line after any of the model validations already present in the model class, you will not be able to override them. An example is already in the model for ShippingAddress (@app/models/shipping_address.rb@), where a site may want to allow specifying "State" to be optional: |
||
157 | |||
158 | <pre> |
||
159 | validates_presence_of :user_id |
||
160 | validates_presence_of :address_line_1 |
||
161 | validates_presence_of :city |
||
162 | validates_presence_of :state |
||
163 | validates_presence_of :zip |
||
164 | validates_presence_of :phone |
||
165 | |||
166 | include SiteSpecific::Validations rescue {} |
||
167 | </pre> |
||
168 | |||
169 | Note that the @include@ is _after_ the list of @validates_presence_of@ directives. This allows any of those validations to be effectively reversed. So, in your site-specific override folder, in your @lib/site_specific/validations.rb@ file (also see example above for Consent Questions), you would simply add the following method: |
||
170 | |||
171 | <pre> |
||
172 | def shipping_address_validations |
||
173 | # allow invalid "state" field |
||
174 | errors.delete(:state) |
||
175 | end |
||
176 | </pre> |
||
177 | 5 | Phil Hodgson | |
178 | 7 | Phil Hodgson | To remove the State field from the user interface entirely you'l also want to remove it from the relevant view, which for ShippingAddress is the partial in your site-override folder under @app/views/shipping_addresses/_form.html.erb@. Simply copy the @_form.html.erb@ file from the original source code tree and then *remove* the following lines: |
179 | |||
180 | <pre> |
||
181 | <div class="field"> |
||
182 | <span style="color: red"> * </span><%= f.label :state %><br /> |
||
183 | <%= f.state_select(:state, 'US', {:include_blank => "Select a State", :selected => @shipping_address.state }, { :style => "width: 230px;"}) -%> |
||
184 | </div> |
||
185 | </pre> |
||
186 | |||
187 | Or remove the @<span style="color: red"> * </span>@ to show it as non-mandatory. (For information on the @state_select@ method, see the documentation for the @carmen@ gem.) |
||
188 | |||
189 | 5 | Phil Hodgson | h4. Automatic Reloading of the Validations Override During Development |
190 | |||
191 | 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: |
||
192 | |||
193 | <pre> |
||
194 | ActiveSupport::Dependencies.explicitly_unloadable_constants << 'SiteSpecific::Validations' |
||
195 | </pre> |
||
196 | 11 | Phil Hodgson | |
197 | h2. Custom Validation of Postal Codes ("Zip" Codes) |
||
198 | |||
199 | Currently for the "Geographic Information Survey" and the "Residency Response" question there are input for a Postal Code. There is a central validation for these Postal Codes. To change it, in your config.yml you can override the config.defaults.yml file key with your own regular expression. Note that you should leave the YAML data type directive in, and also the single quotes (although if they get in the way there are many different ways to do quoting in YAML: check the specifications at http://www.yaml.org/ |
||
200 | |||
201 | <pre> |
||
202 | zip_validation: !ruby/regexp '/^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$/' |
||
203 | </pre> |
||
204 | |||
205 | Next you'll want to change the validation error message. For the "Geographic Information Survey", the Postal Code is saved in the @User@ model in the @zip@ attribute, and this message is currently meant to be used as the default and central message for any Postal Code validation problems. To override, in the locales file location described in [[Internationalization]] add the following key: |
||
206 | |||
207 | <pre> |
||
208 | en: |
||
209 | activerecord: |
||
210 | errors: |
||
211 | models: |
||
212 | user: |
||
213 | attributes: |
||
214 | zip: |
||
215 | invalid: |
||
216 | 'should be in Canadian Postal Code format (A1A-1A1)' |
||
217 | </pre> |