acts_as_addressable 0.1.0

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.
Files changed (75) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +28 -0
  5. data/app/assets/javascripts/acts_as_addressable/application.js +13 -0
  6. data/app/assets/javascripts/acts_as_addressable/locations.js +2 -0
  7. data/app/assets/stylesheets/acts_as_addressable/application.css +15 -0
  8. data/app/assets/stylesheets/acts_as_addressable/locations.css +4 -0
  9. data/app/assets/stylesheets/scaffold.css +56 -0
  10. data/app/controllers/acts_as_addressable/application_controller.rb +4 -0
  11. data/app/controllers/acts_as_addressable/locations_controller.rb +62 -0
  12. data/app/helpers/acts_as_addressable/application_helper.rb +4 -0
  13. data/app/helpers/acts_as_addressable/locations_helper.rb +4 -0
  14. data/app/models/acts_as_addressable/location.rb +33 -0
  15. data/app/views/acts_as_addressable/locations/_form.html.erb +65 -0
  16. data/app/views/acts_as_addressable/locations/edit.html.erb +6 -0
  17. data/app/views/acts_as_addressable/locations/index.html.erb +47 -0
  18. data/app/views/acts_as_addressable/locations/new.html.erb +5 -0
  19. data/app/views/acts_as_addressable/locations/show.html.erb +64 -0
  20. data/app/views/layouts/acts_as_addressable/application.html.erb +14 -0
  21. data/config/routes.rb +4 -0
  22. data/db/migrate/20140426032656_create_acts_as_addressable_locations.rb +23 -0
  23. data/lib/acts_as_addressable.rb +22 -0
  24. data/lib/acts_as_addressable/engine.rb +23 -0
  25. data/lib/acts_as_addressable/local_instance_methods.rb +11 -0
  26. data/lib/acts_as_addressable/version.rb +3 -0
  27. data/lib/tasks/acts_as_addressable_tasks.rake +4 -0
  28. data/spec/acts_as_addressable_spec.rb +23 -0
  29. data/spec/controllers/acts_as_addressable/locations_controller_spec.rb +163 -0
  30. data/spec/factories/acts_as_addressable_locations.rb +31 -0
  31. data/spec/helpers/acts_as_addressable/locations_helper_spec.rb +17 -0
  32. data/spec/models/acts_as_addressable/location_spec.rb +21 -0
  33. data/spec/routing/acts_as_addressable/locations_routing_spec.rb +38 -0
  34. data/spec/spec_helper.rb +54 -0
  35. data/spec/test_app/README.rdoc +28 -0
  36. data/spec/test_app/Rakefile +6 -0
  37. data/spec/test_app/app/assets/javascripts/application.js +13 -0
  38. data/spec/test_app/app/assets/stylesheets/application.css +15 -0
  39. data/spec/test_app/app/controllers/application_controller.rb +5 -0
  40. data/spec/test_app/app/helpers/application_helper.rb +2 -0
  41. data/spec/test_app/app/models/user.rb +3 -0
  42. data/spec/test_app/app/views/layouts/application.html.erb +14 -0
  43. data/spec/test_app/bin/bundle +3 -0
  44. data/spec/test_app/bin/rails +4 -0
  45. data/spec/test_app/bin/rake +4 -0
  46. data/spec/test_app/config.ru +4 -0
  47. data/spec/test_app/config/application.rb +29 -0
  48. data/spec/test_app/config/boot.rb +5 -0
  49. data/spec/test_app/config/database.yml +25 -0
  50. data/spec/test_app/config/environment.rb +5 -0
  51. data/spec/test_app/config/environments/development.rb +37 -0
  52. data/spec/test_app/config/environments/production.rb +83 -0
  53. data/spec/test_app/config/environments/test.rb +39 -0
  54. data/spec/test_app/config/initializers/backtrace_silencers.rb +7 -0
  55. data/spec/test_app/config/initializers/cookies_serializer.rb +3 -0
  56. data/spec/test_app/config/initializers/filter_parameter_logging.rb +4 -0
  57. data/spec/test_app/config/initializers/inflections.rb +16 -0
  58. data/spec/test_app/config/initializers/mime_types.rb +4 -0
  59. data/spec/test_app/config/initializers/session_store.rb +3 -0
  60. data/spec/test_app/config/initializers/wrap_parameters.rb +14 -0
  61. data/spec/test_app/config/locales/en.yml +23 -0
  62. data/spec/test_app/config/routes.rb +4 -0
  63. data/spec/test_app/config/secrets.yml +22 -0
  64. data/spec/test_app/db/development.sqlite3 +0 -0
  65. data/spec/test_app/db/migrate/20140507215937_create_users.rb +9 -0
  66. data/spec/test_app/db/production.sqlite3 +0 -0
  67. data/spec/test_app/db/schema.rb +42 -0
  68. data/spec/test_app/db/test.sqlite3 +0 -0
  69. data/spec/test_app/log/development.log +197 -0
  70. data/spec/test_app/log/test.log +5110 -0
  71. data/spec/test_app/public/404.html +67 -0
  72. data/spec/test_app/public/422.html +67 -0
  73. data/spec/test_app/public/500.html +66 -0
  74. data/spec/test_app/public/favicon.ico +0 -0
  75. metadata +304 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0abd35e31970d82bb8805c51c98cb58931c9f18a
4
+ data.tar.gz: 299cdca16ed38185caf41a867c17716ca18982d4
5
+ SHA512:
6
+ metadata.gz: 79dc17ce334b0ba3242e3bb33c6c8f25682e5a67b151851a5446df681f9b3bc2ac9df849ff54f941fc9f381180c8f5b087d57c89de4f242efe1c2bd07c89d62d
7
+ data.tar.gz: c33688d598651c07074b0bd5d1f4790e27c75f918a798371a6e3700bf021bb0535d29d42cdcf407da99253d9afd6b79599337cf17260254fadf5892b3412237f
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
1
+ = ActsAsAddressable
2
+
3
+ This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,28 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ActsAsAddressable'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/test_app/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
23
+ require 'rspec/core'
24
+ require 'rspec/core/rake_task'
25
+
26
+ desc "Run all specs in spec directory (excluding plugin specs)"
27
+ RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
28
+ task :default => :spec
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,56 @@
1
+ body { background-color: #fff; color: #333; }
2
+
3
+ body, p, ol, ul, td {
4
+ font-family: verdana, arial, helvetica, sans-serif;
5
+ font-size: 13px;
6
+ line-height: 18px;
7
+ }
8
+
9
+ pre {
10
+ background-color: #eee;
11
+ padding: 10px;
12
+ font-size: 11px;
13
+ }
14
+
15
+ a { color: #000; }
16
+ a:visited { color: #666; }
17
+ a:hover { color: #fff; background-color:#000; }
18
+
19
+ div.field, div.actions {
20
+ margin-bottom: 10px;
21
+ }
22
+
23
+ #notice {
24
+ color: green;
25
+ }
26
+
27
+ .field_with_errors {
28
+ padding: 2px;
29
+ background-color: red;
30
+ display: table;
31
+ }
32
+
33
+ #error_explanation {
34
+ width: 450px;
35
+ border: 2px solid red;
36
+ padding: 7px;
37
+ padding-bottom: 0;
38
+ margin-bottom: 20px;
39
+ background-color: #f0f0f0;
40
+ }
41
+
42
+ #error_explanation h2 {
43
+ text-align: left;
44
+ font-weight: bold;
45
+ padding: 5px 5px 5px 15px;
46
+ font-size: 12px;
47
+ margin: -7px;
48
+ margin-bottom: 0px;
49
+ background-color: #c00;
50
+ color: #fff;
51
+ }
52
+
53
+ #error_explanation ul li {
54
+ font-size: 12px;
55
+ list-style: square;
56
+ }
@@ -0,0 +1,4 @@
1
+ module ActsAsAddressable
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,62 @@
1
+ require_dependency "acts_as_addressable/application_controller"
2
+
3
+ module ActsAsAddressable
4
+ class LocationsController < ApplicationController
5
+ before_action :set_location, only: [:show, :edit, :update, :destroy]
6
+
7
+ # GET /locations
8
+ def index
9
+ @locations = Location.all
10
+ end
11
+
12
+ # GET /locations/1
13
+ def show
14
+ end
15
+
16
+ # GET /locations/new
17
+ def new
18
+ @location = Location.new
19
+ end
20
+
21
+ # GET /locations/1/edit
22
+ def edit
23
+ end
24
+
25
+ # POST /locations
26
+ def create
27
+ @location = Location.new(location_params)
28
+
29
+ if @location.save
30
+ redirect_to @location, notice: 'Location was successfully created.'
31
+ else
32
+ render :new
33
+ end
34
+ end
35
+
36
+ # PATCH/PUT /locations/1
37
+ def update
38
+ if @location.update(location_params)
39
+ redirect_to @location, notice: 'Location was successfully updated.'
40
+ else
41
+ render :edit
42
+ end
43
+ end
44
+
45
+ # DELETE /locations/1
46
+ def destroy
47
+ @location.destroy
48
+ redirect_to locations_url, notice: 'Location was successfully destroyed.'
49
+ end
50
+
51
+ private
52
+ # Use callbacks to share common setup or constraints between actions.
53
+ def set_location
54
+ @location = Location.find(params[:id])
55
+ end
56
+
57
+ # Only allow a trusted parameter "white list" through.
58
+ def location_params
59
+ params.require(:location).permit(:country_code, :street_1, :street_2, :city, :region, :postal_code, :latitude, :longitude, :addressable_id, :addressable_type, :mailing, :physical, :primary)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,4 @@
1
+ module ActsAsAddressable
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ActsAsAddressable
2
+ module LocationsHelper
3
+ end
4
+ end
@@ -0,0 +1,33 @@
1
+ module ActsAsAddressable
2
+ class Location < ActiveRecord::Base
3
+ belongs_to :addressable, polymorphic: true
4
+
5
+ validates_presence_of :city, :postal_code, :street_1
6
+ validate :multiple_primary, if: "self.primary?"
7
+
8
+ scope :primary, -> { where primary: true }
9
+
10
+ def self.for_addressable(location)
11
+ where(addressable_id: location.addressable_id).where(addressable_type: location.addressable_type)
12
+ end
13
+
14
+ def has_primary?
15
+ !!primary_location_for_addressable
16
+ end
17
+
18
+ def primary_location_for_addressable
19
+ @primary_location_for_addressable ||= Location.primary.for_addressable(self).first
20
+ end
21
+
22
+ private
23
+
24
+ def multiple_primary
25
+ if has_primary?
26
+ unless self.id && self.id == primary_location_for_addressable.id
27
+ errors.add(:base, 'A primary address already exists.')
28
+ end
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,65 @@
1
+ <%= form_for(@location) do |f| %>
2
+ <% if @location.errors.any? %>
3
+ <div id="error_explanation">
4
+ <h2><%= pluralize(@location.errors.count, "error") %> prohibited this location from being saved:</h2>
5
+
6
+ <ul>
7
+ <% @location.errors.full_messages.each do |message| %>
8
+ <li><%= message %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+
14
+ <div class="field">
15
+ <%= f.label :country_code %><br>
16
+ <%= f.text_field :country_code %>
17
+ </div>
18
+ <div class="field">
19
+ <%= f.label :street_1 %><br>
20
+ <%= f.text_field :street_1 %>
21
+ </div>
22
+ <div class="field">
23
+ <%= f.label :street_2 %><br>
24
+ <%= f.text_field :street_2 %>
25
+ </div>
26
+ <div class="field">
27
+ <%= f.label :city %><br>
28
+ <%= f.text_field :city %>
29
+ </div>
30
+ <div class="field">
31
+ <%= f.label :region %><br>
32
+ <%= f.text_field :region %>
33
+ </div>
34
+ <div class="field">
35
+ <%= f.label :postal_code %><br>
36
+ <%= f.text_field :postal_code %>
37
+ </div>
38
+ <div class="field">
39
+ <%= f.label :latitude %><br>
40
+ <%= f.text_field :latitude %>
41
+ </div>
42
+ <div class="field">
43
+ <%= f.label :longitude %><br>
44
+ <%= f.text_field :longitude %>
45
+ </div>
46
+ <div class="field">
47
+ <%= f.label :addressable_id %><br>
48
+ <%= f.text_field :addressable_id %>
49
+ </div>
50
+ <div class="field">
51
+ <%= f.label :mailing %><br>
52
+ <%= f.text_field :mailing %>
53
+ </div>
54
+ <div class="field">
55
+ <%= f.label :physical %><br>
56
+ <%= f.check_box :physical %>
57
+ </div>
58
+ <div class="field">
59
+ <%= f.label :primary %><br>
60
+ <%= f.check_box :primary %>
61
+ </div>
62
+ <div class="actions">
63
+ <%= f.submit %>
64
+ </div>
65
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <h1>Editing location</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Show', @location %> |
6
+ <%= link_to 'Back', locations_path %>
@@ -0,0 +1,47 @@
1
+ <h1>Listing locations</h1>
2
+
3
+ <table>
4
+ <thead>
5
+ <tr>
6
+ <th>Country code</th>
7
+ <th>Street 1</th>
8
+ <th>Street 2</th>
9
+ <th>City</th>
10
+ <th>Region</th>
11
+ <th>Postal code</th>
12
+ <th>Latitude</th>
13
+ <th>Longitude</th>
14
+ <th>Addressable</th>
15
+ <th>Mailing</th>
16
+ <th>Physical</th>
17
+ <th>Primary</th>
18
+ <th colspan="3"></th>
19
+ </tr>
20
+ </thead>
21
+
22
+ <tbody>
23
+ <% @locations.each do |location| %>
24
+ <tr>
25
+ <td><%= location.country_code %></td>
26
+ <td><%= location.street_1 %></td>
27
+ <td><%= location.street_2 %></td>
28
+ <td><%= location.city %></td>
29
+ <td><%= location.region %></td>
30
+ <td><%= location.postal_code %></td>
31
+ <td><%= location.latitude %></td>
32
+ <td><%= location.longitude %></td>
33
+ <td><%= location.addressable %></td>
34
+ <td><%= location.mailing %></td>
35
+ <td><%= location.physical %></td>
36
+ <td><%= location.primary %></td>
37
+ <td><%= link_to 'Show', location %></td>
38
+ <td><%= link_to 'Edit', edit_location_path(location) %></td>
39
+ <td><%= link_to 'Destroy', location, method: :delete, data: { confirm: 'Are you sure?' } %></td>
40
+ </tr>
41
+ <% end %>
42
+ </tbody>
43
+ </table>
44
+
45
+ <br>
46
+
47
+ <%= link_to 'New Location', new_location_path %>
@@ -0,0 +1,5 @@
1
+ <h1>New location</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Back', locations_path %>
@@ -0,0 +1,64 @@
1
+ <p id="notice"><%= notice %></p>
2
+
3
+ <p>
4
+ <strong>Country code:</strong>
5
+ <%= @location.country_code %>
6
+ </p>
7
+
8
+ <p>
9
+ <strong>Street 1:</strong>
10
+ <%= @location.street_1 %>
11
+ </p>
12
+
13
+ <p>
14
+ <strong>Street 2:</strong>
15
+ <%= @location.street_2 %>
16
+ </p>
17
+
18
+ <p>
19
+ <strong>City:</strong>
20
+ <%= @location.city %>
21
+ </p>
22
+
23
+ <p>
24
+ <strong>Region:</strong>
25
+ <%= @location.region %>
26
+ </p>
27
+
28
+ <p>
29
+ <strong>Postal code:</strong>
30
+ <%= @location.postal_code %>
31
+ </p>
32
+
33
+ <p>
34
+ <strong>Latitude:</strong>
35
+ <%= @location.latitude %>
36
+ </p>
37
+
38
+ <p>
39
+ <strong>Longitude:</strong>
40
+ <%= @location.longitude %>
41
+ </p>
42
+
43
+ <p>
44
+ <strong>Addressable:</strong>
45
+ <%= @location.addressable %>
46
+ </p>
47
+
48
+ <p>
49
+ <strong>Mailing:</strong>
50
+ <%= @location.mailing %>
51
+ </p>
52
+
53
+ <p>
54
+ <strong>Physical:</strong>
55
+ <%= @location.physical %>
56
+ </p>
57
+
58
+ <p>
59
+ <strong>Primary:</strong>
60
+ <%= @location.primary %>
61
+ </p>
62
+
63
+ <%= link_to 'Edit', edit_location_path(@location) %> |
64
+ <%= link_to 'Back', locations_path %>