Customization » History » Version 6
Phil Hodgson, 05/22/2014 12:30 PM
1 | 1 | Phil Hodgson | h1. Customization |
---|---|---|---|
2 | |||
3 | 6 | Phil Hodgson | h2. Enabling and Disabling "Sections" of Tapestry |
4 | |||
5 | Tapestry has been divided into a handful of logical "sections". To date, they are: |
||
6 | |||
7 | * Section::SIGNUP |
||
8 | * Section::PUBLIC_DATA |
||
9 | * Section::ENROLL |
||
10 | * Section::GOOGLE_SURVEYS |
||
11 | * Section::SAMPLES |
||
12 | |||
13 | 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. |
||
14 | |||
15 | 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. |
||
16 | |||
17 | 1 | Phil Hodgson | h2. Overriding Default Views, Partials, Templates, etc. |
18 | 2 | Phil Hodgson | |
19 | 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@. |
||
20 | 1 | Phil Hodgson | |
21 | 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. |
22 | 2 | Phil Hodgson | |
23 | 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. |
||
24 | 3 | Phil Hodgson | |
25 | h3. Caveat |
||
26 | |||
27 | _This statement to be followed up after more investigation._ |
||
28 | |||
29 | 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. |
||
30 | |||
31 | h3. Overriding @lib@ Files |
||
32 | |||
33 | 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@. |
||
34 | |||
35 | h2. Adding Custom Questions to the "Participation Consent" Form |
||
36 | |||
37 | 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. |
||
38 | |||
39 | There is a view helper for creating radio boxes for the participation concept form using this "other_answers" field. Example: |
||
40 | |||
41 | <pre> |
||
42 | <div class="consent-form-question"> |
||
43 | <p> |
||
44 | Would you judge yourself to be sane? |
||
45 | <%= radio_answers( 'sanity', [['0', 'No'], |
||
46 | ['1', 'Sometimes'], |
||
47 | ['2', 'Yes']] ) %> |
||
48 | </p> |
||
49 | </div> |
||
50 | </pre> |
||
51 | |||
52 | 4 | Phil Hodgson | There are also helpers for text areas (@text_area_answer@) and standard input texts (@text_field_answer@). For example: |
53 | 3 | Phil Hodgson | |
54 | 4 | Phil Hodgson | <pre> |
55 | <div class="consent-form-question"> |
||
56 | <p> |
||
57 | If sane only sometimes, please explain when and for what reason this occurs: |
||
58 | </p> |
||
59 | <p> |
||
60 | <%= text_area_answer 'reason_sometimes_sane', { :cols => 60, :rows => 3 } %> |
||
61 | </p> |
||
62 | </div> |
||
63 | </pre> |
||
64 | |||
65 | |||
66 | 3 | Phil Hodgson | h3. Adding Custom Validation of Your Custom Question |
67 | 1 | Phil Hodgson | |
68 | 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: |
69 | 3 | Phil Hodgson | |
70 | 1 | Phil Hodgson | <pre> |
71 | 4 | Phil Hodgson | module SiteSpecific |
72 | module Validations |
||
73 | extend ActiveSupport::Concern |
||
74 | |||
75 | # *Do not remove this +included+ block!* It is what works the magic. |
||
76 | included do |
||
77 | method_name = "#{self.name.to_s.underscore}_validations" |
||
78 | validate method_name if method_defined? method_name |
||
79 | end |
||
80 | |||
81 | end |
||
82 | end |
||
83 | </pre> |
||
84 | |||
85 | 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: |
||
86 | |||
87 | <pre> |
||
88 | 1 | Phil Hodgson | def informed_consent_response_validations |
89 | 4 | Phil Hodgson | case self.other_answers[:sanity] |
90 | when '2' |
||
91 | 1 | Phil Hodgson | errors.add( :other_answers, :sanity_not_permitted) |
92 | 4 | Phil Hodgson | when '1' |
93 | if self.other_answers[:reason_sometimes_sane].blank? |
||
94 | errors.add( :other_answers, :explain_occasional_sanity ) |
||
95 | end |
||
96 | 3 | Phil Hodgson | end |
97 | 1 | Phil Hodgson | end |
98 | 3 | Phil Hodgson | </pre> |
99 | |||
100 | 4 | Phil Hodgson | The messages should be placed in your locale file under: |
101 | 3 | Phil Hodgson | |
102 | <pre> |
||
103 | en: |
||
104 | activerecord: |
||
105 | errors: |
||
106 | 1 | Phil Hodgson | models: |
107 | informed_consent_response: |
||
108 | attributes: |
||
109 | other_answers: |
||
110 | 4 | Phil Hodgson | sanity_not_permitted: |
111 | You are not permitted to be sane. |
||
112 | explain_occasional_sanity: |
||
113 | Please explain when and why you are sometimes sane. |
||
114 | 3 | Phil Hodgson | </pre> |
115 | 4 | Phil Hodgson | |
116 | 1 | Phil Hodgson | See the documentation on [[Internationalization]] for where to put this. |
117 | 6 | Phil Hodgson | |
118 | h2. Overriding the Validations on Any Model |
||
119 | |||
120 | 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: |
||
121 | |||
122 | <pre> |
||
123 | include SiteSpecific::Validations rescue {} |
||
124 | </pre> |
||
125 | |||
126 | 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: |
||
127 | |||
128 | <pre> |
||
129 | validates_presence_of :user_id |
||
130 | validates_presence_of :address_line_1 |
||
131 | validates_presence_of :city |
||
132 | validates_presence_of :state |
||
133 | validates_presence_of :zip |
||
134 | validates_presence_of :phone |
||
135 | |||
136 | include SiteSpecific::Validations rescue {} |
||
137 | </pre> |
||
138 | |||
139 | 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: |
||
140 | |||
141 | <pre> |
||
142 | def shipping_address_validations |
||
143 | # allow invalid "state" field |
||
144 | errors.delete(:state) |
||
145 | end |
||
146 | </pre> |
||
147 | 5 | Phil Hodgson | |
148 | h4. Automatic Reloading of the Validations Override During Development |
||
149 | |||
150 | 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: |
||
151 | |||
152 | <pre> |
||
153 | ActiveSupport::Dependencies.explicitly_unloadable_constants << 'SiteSpecific::Validations' |
||
154 | </pre> |