Skip to content

Commit 1b0d210

Browse files
authored
Merge pull request #3 from DaleLore/Csv
Clean up
2 parents 0289e4b + e5c1d5a commit 1b0d210

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed

Gemfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ gem 'rails', '~> 5.2.3'
99
gem 'pg', '>= 0.18', '< 2.0'
1010
# Use Puma as the app server
1111
gem 'puma', '~> 3.11'
12+
13+
#Responders for the CSV: https://edgeguides.rubyonrails.org/4_2_release_notes.html
14+
#Only did it because of this ques: https://stackoverflow.com/questions/35983628/undefined-instance-method-respond-to-in-rails-5-api-controller
15+
gem 'responders', '~> 2.0'
16+
1217
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
1318
# gem 'jbuilder', '~> 2.5'
1419
# Use Redis adapter to run Action Cable in production
@@ -17,6 +22,8 @@ gem 'puma', '~> 3.11'
1722
# Use ActiveModel has_secure_password
1823
gem 'bcrypt', '~> 3.1.7'
1924

25+
#CSV Installation: https://github.com/ruby/csv
26+
gem 'csv'
2027
# Use ActiveStorage variant
2128
# gem 'mini_magick', '~> 4.8'
2229

Gemfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ GEM
5757
activesupport
5858
concurrent-ruby (1.1.5)
5959
crass (1.0.4)
60+
csv (3.1.1)
6061
erubi (1.8.0)
6162
ffi (1.11.1)
6263
globalid (0.4.2)
@@ -119,6 +120,9 @@ GEM
119120
rb-fsevent (0.10.3)
120121
rb-inotify (0.10.0)
121122
ffi (~> 1.0)
123+
responders (2.4.1)
124+
actionpack (>= 4.2.0, < 6.0)
125+
railties (>= 4.2.0, < 6.0)
122126
ruby_dep (1.5.0)
123127
spring (2.1.0)
124128
spring-watcher-listen (2.0.1)
@@ -147,12 +151,14 @@ DEPENDENCIES
147151
bcrypt (~> 3.1.7)
148152
bootsnap (>= 1.1.0)
149153
byebug
154+
csv
150155
jwt
151156
listen (>= 3.0.5, < 3.2)
152157
pg (>= 0.18, < 2.0)
153158
puma (~> 3.11)
154159
rack-cors
155160
rails (~> 5.2.3)
161+
responders (~> 2.0)
156162
spring
157163
spring-watcher-listen (~> 2.0.0)
158164
tzinfo-data

app/models/collection.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
class Collection < ApplicationRecord
22
belongs_to :user
33
has_many :items, dependent: :destroy
4+
5+
def self.to_csv(fields = column_names, options = {})
6+
CSV.generate(options) do |csv|
7+
csv << fields
8+
all.each do |collection|
9+
csv << collection.attributes.values_at(*fields)
10+
end
11+
end
12+
end
13+
414
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<legend>Collections</legend>
2+
3+
<%= form_tag import_products_path, multipart: true, class: 'form-inline' do %>
4+
<div class="form-group">
5+
<%= link_to "Export CSV", products_path(format: "csv"), class: 'btn btn-primary' %>
6+
</div>
7+
8+
<div class="form-group">
9+
<%= file_field_tag :file, class: '' %>
10+
</div>
11+
12+
<div class="form-group">
13+
<%= submit_tag "Import CSV", class: 'btn btn-info' %>
14+
</div>
15+
<% end %>
16+
17+
<br/>
18+
19+
<table class='table table-bordered table-condensed table-striped table-hover'>
20+
<thead>
21+
<tr>
22+
<th>Collection Name</th>
23+
<th>Description</th>
24+
<th colspan="2"></th>
25+
</tr>
26+
</thead>
27+
28+
<tbody>
29+
<% @collections.each do |collection| %>
30+
<tr>
31+
<td><%= collection.collection_name %></td>
32+
<td><%= collection.description %></td>
33+
<td><%= link_to 'Show', product %></td>
34+
<td><%= link_to 'Edit', edit_product_path(product) %></td>
35+
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
36+
</tr>
37+
<% end %>
38+
</tbody>
39+
</table>
40+
41+
<br>

config/application.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
require "action_mailer/railtie"
1111
require "action_view/railtie"
1212
require "action_cable/engine"
13+
require "csv"
1314
# require "sprockets/railtie"
1415
# require "rails/test_unit/railtie"
1516

0 commit comments

Comments
 (0)