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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3e9802b47854be9086f22ad21c9a8e2447cd0d17
4
- data.tar.gz: 2b7210fe13d6de4e4553b016202d445e3f3945a3
3
+ metadata.gz: 4481d16542867faa48ecaa8cd9c7e2854275a1da
4
+ data.tar.gz: c5df052d9bb71ab982f0dbe08c3065f6724aad5b
5
5
  SHA512:
6
- metadata.gz: 96df5a8f2099813eb27c730fdd5e712f0e03cd28ccd79aa671b8bd9a2dfb80d763557a29d3c25ea519ef766bfa34a67e2cfa2b14d90497b9a1c7c69d97bdb22d
7
- data.tar.gz: 55b2403e91718c8c3848651733bf0f3d9894b27e0976a5f2de40d7e310a6b491f7690b8f5649f37aeda70cfc34271a3e0a737f9dbc91d36500e71f48999fff20
6
+ metadata.gz: 144bb81bbe6771830dcbdf40cecaa7c099ea9203874afac9764067cb9a7910044b0e6814f0a836755e4e793aa6f99e9dd30592db914d35b6856383e63ca516fb
7
+ data.tar.gz: 9b03149169736299be636f3b094d465cfdf07237472fc686cb00e3ae639fa2c53b5022c6b086038061caf2f34a253b5f0cdc4cd0bfd68ad2a13cbbdfbf6f6a73
@@ -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
- Access information about geographic divisions in Rwanda from within the Rails framework.
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
  [![Gem Version](https://badge.fury.io/rb/rwanda-rails.svg)](http://badge.fury.io/rb/rwanda-rails)
@@ -10,82 +10,63 @@ class Rwanda
10
10
  ActionView::Helpers::FormBuilder.send(:include, FormBuilder)
11
11
  end
12
12
 
13
- def rwanda_location(object_name, location, options={})
14
- cleaned_options = { include_blank: 'Unknown' }
15
- cleaned_options[:onchange] = "this.form.submit();" if options[:autosubmit]
16
- cleaned_options.merge!(options.select {|k,v| [ :force_edit, :table ].include? k })
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 = cleaned_options[:table] ? "<table border=0>\n".html_safe : ''.html_safe
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 = options[: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 cleaned_options[:table]
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(cleaned_options)
48
+ subdivision_options = { selected: div }.merge(select_options)
32
49
  output <<
33
- select_tag("#{object_name}[#{level}]", options_for_select(subdivisions, div), cleaned_options) <<
34
- " #{level.humanize}".html_safe
35
- output << "</td><td></td>\n".html_safe if cleaned_options[:table]
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&nbsp;#{'</td><td>' if cleaned_options[:table]}".html_safe <<
40
- link_to("[Change this]", edit_company_location_path(force_edit: level))
56
+ "<b>#{div}</b> #{level.to_s.humanize}\n&nbsp;#{'</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 cleaned_options[:table]
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 cleaned_options[:table]
64
+ output << "</td></tr>\n".html_safe if config[:table]
48
65
  end
49
- output << "</table>\n".html_safe if cleaned_options[:table]
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
@@ -1,5 +1,5 @@
1
1
  class Rwanda
2
2
  module Rails
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rwanda-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Hetherington