rwanda-rails 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/README.md +28 -3
- data/lib/rwanda/rails.rb +34 -53
- data/lib/rwanda/rails/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4481d16542867faa48ecaa8cd9c7e2854275a1da
|
4
|
+
data.tar.gz: c5df052d9bb71ab982f0dbe08c3065f6724aad5b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 144bb81bbe6771830dcbdf40cecaa7c099ea9203874afac9764067cb9a7910044b0e6814f0a836755e4e793aa6f99e9dd30592db914d35b6856383e63ca516fb
|
7
|
+
data.tar.gz: 9b03149169736299be636f3b094d465cfdf07237472fc686cb00e3ae639fa2c53b5022c6b086038061caf2f34a253b5f0cdc4cd0bfd68ad2a13cbbdfbf6f6a73
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,14 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 0.1.3
|
4
|
+
|
5
|
+
* Significant corrections and added detail to README
|
6
|
+
* Reference example application at Github
|
7
|
+
|
8
|
+
## 0.1.2
|
9
|
+
|
10
|
+
* Remmove inadvertent references to tracker models (first testing with an empty application)
|
11
|
+
|
3
12
|
## 0.1.1
|
4
13
|
|
5
14
|
* Allow rwanda version to increment
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Rwanda::Rails
|
2
2
|
|
3
|
-
|
3
|
+
Add information about geographic location within Rwanda to models in the Rails framework, and update them through a web interface.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -12,7 +12,7 @@ gem 'rwanda-rails'
|
|
12
12
|
|
13
13
|
And then execute:
|
14
14
|
|
15
|
-
$ bundle
|
15
|
+
$ bundle install
|
16
16
|
|
17
17
|
Or install it yourself as:
|
18
18
|
|
@@ -20,10 +20,11 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
In the view:
|
23
|
+
In the view (e.g. in `_form.html.erb` if you generated your model using `rails generate scaffold`):
|
24
24
|
|
25
25
|
```erb
|
26
26
|
<%= form_for(@person) %>
|
27
|
+
<% loc = Location.new(@company.district, @company.sector, @company.cell, @company.village) %>
|
27
28
|
<%# "force_edit: @force_edit" is necessary in order to be able to edit previously filled-in fields %>
|
28
29
|
<%= f.rwanda_location(loc, { force_edit: @force_edit, autosubmit: true }) %>
|
29
30
|
<%= f.submit 'Update Location' %>
|
@@ -33,17 +34,41 @@ In the controller:
|
|
33
34
|
|
34
35
|
```ruby
|
35
36
|
# Can be in its own action instead of edit if you prefer
|
37
|
+
# However, you would then have to pass the action in to rwanda_location in the options hash
|
36
38
|
def edit
|
37
39
|
@force_edit = params[:force_edit]
|
38
40
|
end
|
41
|
+
|
42
|
+
def update
|
43
|
+
respond_to do |format|
|
44
|
+
if @person.update(person_params)
|
45
|
+
# by rendering edit instead of show, it is possible to quickly enter all levels
|
46
|
+
# one after the other
|
47
|
+
format.html { render :edit, notice: 'Person was successfully updated.' }
|
48
|
+
format.json { render :edit, status: :ok, location: @person }
|
49
|
+
else
|
50
|
+
format.html { render :edit }
|
51
|
+
format.json { render json: @person.errors, status: :unprocessable_entity }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
39
55
|
```
|
40
56
|
|
41
57
|
The model to which you're adding a Rwandan location needs attributes `district`, `sector`, `cell` and `village`.
|
42
58
|
|
43
59
|
```
|
44
60
|
rails generate migration add_location_to_person district:string sector:string cell:string village:string
|
61
|
+
rake db:migrate
|
45
62
|
```
|
46
63
|
|
64
|
+
## Edit location in `edit` view, or create a separate view and action?
|
65
|
+
|
66
|
+
Entering a full Rwandan location (district, sector, cell and village) requires a page load at each step (CSV data covering all of Rwanda is around 0.75 MB -- a significant download, so instead only that which is needed is provided by the server). To make this less clunky, `autosubmit: true` can be passed to speed up the process by submitting the form as soon as a district, sector, etc is chosen, reducing the number of clicks. However, this requires that submission of the `_form` return the user to the `edit` view rather than, as is more traditional, a `show` view. For these reasons, it might be preferable to use an "Edit Location" link in your edit view to go to a separate edit_location action and view. Then the original `edit` action can behave as expected, i.e. when submitted it will update the record and forward the user to a `show`. However, either way works.
|
67
|
+
|
68
|
+
## Example application
|
69
|
+
|
70
|
+
Take a look at a [minimal Rails application that uses this gem](https://github.com/repent/example-rwanda-rails).
|
71
|
+
|
47
72
|
## Status
|
48
73
|
|
49
74
|
[](http://badge.fury.io/rb/rwanda-rails)
|
data/lib/rwanda/rails.rb
CHANGED
@@ -10,82 +10,63 @@ class Rwanda
|
|
10
10
|
ActionView::Helpers::FormBuilder.send(:include, FormBuilder)
|
11
11
|
end
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
# available options:
|
14
|
+
# required:
|
15
|
+
# :force_edit - name of level that has specifically been requested to be changed by the user
|
16
|
+
# optional:
|
17
|
+
# :autosubmit - submit the form immediately rather than waiting for the button to be pressed
|
18
|
+
# :include_blank - string to display if nothing selected, defaults to 'Unknown'
|
19
|
+
# :table - output in a <table> if true
|
20
|
+
# #:edit_path - defaults to edit_model_path
|
21
|
+
# :action - defaults to edit
|
22
|
+
|
23
|
+
def rwanda_location(object_name, location, raw_options={})
|
24
|
+
# transfer everything from raw_options into config for random access
|
25
|
+
config = { table: true, action: 'edit' }.merge(raw_options)
|
26
|
+
|
27
|
+
# pick out only those raw_options that will be passed to select_tag
|
28
|
+
select_options = { include_blank: 'Unknown' }.merge(raw_options.select {|k,v| [ :force_edit, :include_blank ].include? k })
|
29
|
+
select_options[:onchange] = "this.form.submit();" if raw_options[:autosubmit]
|
30
|
+
|
17
31
|
# object_name = company
|
18
32
|
# location = Struct.new( :division ... )
|
19
|
-
output =
|
33
|
+
output = config[:table] ? "<table border=0>\n".html_safe : ''.html_safe
|
20
34
|
# location is a Location object defined in the rails gem
|
21
35
|
location.validate! # get rid of any erroneous data -- no more checks necessary
|
22
|
-
force_edit =
|
36
|
+
force_edit = config[:force_edit] ? config[:force_edit].to_sym : nil
|
37
|
+
#binding.pry
|
38
|
+
#cleaned_options[:edit_path] = options[:edit_path] || edit_polymorphic_path(object_name, force_edit: force_edit)
|
23
39
|
location.each_pair do |level, div|
|
24
|
-
output << "<tr><td>\n".html_safe if
|
40
|
+
output << "<tr><td>\n".html_safe if config[:table]
|
25
41
|
output << "<div class=\"field\">\n".html_safe
|
42
|
+
#binding.pry
|
26
43
|
if force_edit == level || ( location.first_missing == level && !force_edit )
|
27
44
|
# editable
|
28
45
|
# Want subdivisions of [district] if level is sector
|
29
46
|
subdivisions = Rwanda.instance.subdivisions_of(location.top(Location.index_of(level)-1))
|
30
47
|
|
31
|
-
subdivision_options = { selected: div }.merge(
|
48
|
+
subdivision_options = { selected: div }.merge(select_options)
|
32
49
|
output <<
|
33
|
-
select_tag("#{object_name}[#{level}]", options_for_select(subdivisions, div),
|
34
|
-
" #{level.humanize}".html_safe
|
35
|
-
output << "</td><td></td>\n".html_safe if
|
50
|
+
select_tag("#{object_name}[#{level}]", options_for_select(subdivisions, div), select_options) <<
|
51
|
+
" #{level.to_s.humanize}".html_safe
|
52
|
+
output << "</td><td></td>\n".html_safe if config[:table]
|
36
53
|
elsif div
|
37
54
|
# display
|
38
55
|
output <<
|
39
|
-
"<b>#{div}</b> #{level.humanize}\n #{'</td><td>' if
|
40
|
-
link_to("[Change this]",
|
56
|
+
"<b>#{div}</b> #{level.to_s.humanize}\n #{'</td><td>' if config[:table]}".html_safe <<
|
57
|
+
link_to("[Change this]", polymorphic_path(object_name, action: config[:action], force_edit: level))
|
41
58
|
else
|
42
59
|
# greyed out message
|
43
|
-
output << "Enter data above before selecting #{level.humanize}\n"
|
44
|
-
output << "</td><td></td>\n".html_safe if
|
60
|
+
output << "Enter data above before selecting #{level.to_s.humanize}\n"
|
61
|
+
output << "</td><td></td>\n".html_safe if config[:table]
|
45
62
|
end
|
46
63
|
output << "</div>\n".html_safe
|
47
|
-
output << "</td></tr>\n".html_safe if
|
64
|
+
output << "</td></tr>\n".html_safe if config[:table]
|
48
65
|
end
|
49
|
-
output << "</table>\n".html_safe if
|
66
|
+
output << "</table>\n".html_safe if config[:table]
|
50
67
|
#location.to_s
|
51
68
|
output
|
52
69
|
end
|
53
|
-
|
54
|
-
#private
|
55
|
-
#def division(division, company, force_edit=false)
|
56
|
-
# #logger.debug "force_edit: #{force_edit} (#{force_edit.class.to_s}); division: #{division} (#{division.class.to_s})"
|
57
|
-
# force_edit &&= force_edit.to_symbol
|
58
|
-
# # all higher divisions validate
|
59
|
-
# if validate_higher(division, company)
|
60
|
-
# # this division validates
|
61
|
-
# if validate_this(division, company)
|
62
|
-
# # user has asked to edit this division
|
63
|
-
# if force_edit == division
|
64
|
-
# #> drop-down
|
65
|
-
# return true
|
66
|
-
# # user hasn't asked to edit
|
67
|
-
# else
|
68
|
-
# #> display as text with link to edit
|
69
|
-
# return false
|
70
|
-
# end
|
71
|
-
# # this division doesn't validate
|
72
|
-
# else
|
73
|
-
# # another division has been forced
|
74
|
-
# if force_edit && force_edit != division
|
75
|
-
# #> greyed out
|
76
|
-
# return false
|
77
|
-
# else
|
78
|
-
# #> drop-down
|
79
|
-
# return true
|
80
|
-
# end
|
81
|
-
# end
|
82
|
-
# # at least one higher division doesn't validate
|
83
|
-
# else
|
84
|
-
# # greyed out message
|
85
|
-
# return false
|
86
|
-
# end
|
87
|
-
#end
|
88
|
-
#end
|
89
70
|
end
|
90
71
|
|
91
72
|
module FormBuilder
|
data/lib/rwanda/rails/version.rb
CHANGED