Skip to content

Commit

Permalink
csv handler
Browse files Browse the repository at this point in the history
  • Loading branch information
rtsinani committed Jul 26, 2015
1 parent 1de906a commit 56ae941
Show file tree
Hide file tree
Showing 19 changed files with 235 additions and 147 deletions.
2 changes: 1 addition & 1 deletion amazing/app/assets/javascripts/books.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ app.Books.prototype = {
_initAutocomplete: function() {
this._input
.autocomplete({
source: '/books',
source: '/books/search',
appendTo: '#books-search-results',
select: $.proxy(this._select, this)
})
Expand Down
196 changes: 116 additions & 80 deletions amazing/app/assets/stylesheets/books.scss
Original file line number Diff line number Diff line change
@@ -1,98 +1,134 @@
.page-books {
&.index {
.books-search {
text-align: center;
max-width: 35em;
margin: 2em auto;
padding: 2em;

input {
width: 100%;
font-size: 1em;
padding: .5em 1.3em;
background: transparent;
border: 1px solid #ddd;
border-radius: 2em;

&:focus {
border-color: #A5CEF5;
}
.books-search {
text-align: center;
max-width: 35em;
margin: 2em auto;
padding: 2em;

input {
width: 100%;
font-size: 1em;
padding: .5em 1.3em;
background: transparent;
border: 1px solid #ddd;
border-radius: 2em;

&:focus {
border-color: #A5CEF5;
}
}

.results {
text-align: left;

.results {
text-align: left;

.ui-widget-content {
background: #fff;
font-size: .9em;
padding: .5em 0;
border-color: #ddd;
box-shadow: 0 .1em .2em rgba(187, 187, 187, 0.64);
line-height: 1.2;
max-height: 20em;
.ui-widget-content {
background: #fff;
font-size: .9em;
padding: .5em 0;
border-color: #ddd;
box-shadow: 0 .1em .2em rgba(187, 187, 187, 0.64);
line-height: 1.2;
max-height: 20em;
overflow: hidden;
overflow-y: auto;
}

.ui-menu-item {
font-family: 'Helvetica Neue', Helvetica, sans-serif;
padding: .4em .6em .4em 6.2em;
position: relative;
border: 0;
border-left: 5px solid transparent;
position: relative;
height: 6.5em;

.img {
position: absolute;
width: 5em;
height: 6em;
top: .4em;
left: .6em;
overflow: hidden;
overflow-y: auto;
}

.ui-menu-item {
font-family: 'Helvetica Neue', Helvetica, sans-serif;
padding: .4em .6em .4em 6.2em;
position: relative;
border: 0;
border-left: 5px solid transparent;
position: relative;
height: 6.5em;

.img {
position: absolute;
width: 5em;
height: 6em;
top: .4em;
left: .6em;
overflow: hidden;

img {
max-width: 100%;
}
img {
max-width: 100%;
}
}

.title {
display: block;
color: #555;
}
.title {
display: block;
color: #555;
}

.author {
display: block;
font-size: .8em;
text-transform: uppercase;
color: #aaa;
margin-top: .6em;
letter-spacing: 1px;
}
.author {
display: block;
font-size: .8em;
text-transform: uppercase;
color: #aaa;
margin-top: .6em;
letter-spacing: 1px;
}

.price {
display: block;
font-size: .8em;
color: #aaa;
margin-top: .5em;
}
.price {
display: block;
font-size: .8em;
color: #aaa;
margin-top: .5em;
}
}

.ui-state-focus {
background: #fff;
border-left-color: #08c;
font-weight: 300;
.ui-state-focus {
background: #fff;
border-left-color: #08c;
font-weight: 300;

.title {
color: #08c;
}
.title {
color: #08c;
}

.price,
.author {
color: #08c;
}
.price,
.author {
color: #08c;
}
}
}
}

.books-list {
.book {
padding-left: 12em;
height: 13em;
position: relative;
line-height: 1.2;
}

.img {
position: absolute;
left: 0;
width: 10em;

img {
max-width: 100%;
}
}

.title {
font-size: 1.3em;
}

.author {
color: #888;
margin-bottom: .4em;
}

.details {
color: #aaa;
font-size: .9em;

span {
display: inline-block;
margin-right: 1em;
}
}
}
}
12 changes: 12 additions & 0 deletions amazing/app/assets/stylesheets/header.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,17 @@

.logo {
font-size: 1.3em;
float: left;
}

.nav {
float: right;

li {
display: inline-block;
font-size: .7em;
text-transform: uppercase;
letter-spacing: 1px;
}
}
}
2 changes: 2 additions & 0 deletions amazing/app/assets/stylesheets/layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ a {
float: right;
text-decoration: underline;
font-size: .9em;
display: inline-block;
margin-left: 1em;
}
}
}
4 changes: 4 additions & 0 deletions amazing/app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class BooksController < ApplicationController
# GET /books
# GET /books.json
def index
@books = Book.order(:title).limit(50)
end

def search
respond_to do |format|
format.html
format.json { @books = Book.search(params[:term]) }
Expand Down
3 changes: 3 additions & 0 deletions amazing/app/controllers/handlers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class HandlersController < ApplicationController
def index
@books = []
@books << Book.new(title: 'Da Vinci Code', author: 'Dan Brown')
@books << Book.new(title: 'Name of the Rose', author: 'Umberto Eco')
end
end
11 changes: 11 additions & 0 deletions amazing/app/views/books/index.csv.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
csv << ['Title', 'Author', 'Price', 'Publication date', 'Image url']

@books.each do |book|
csv << [
book.title,
book.author,
number_to_currency(book.price),
book.published_at.strftime('%B %Y'),
book.image_url
]
end
23 changes: 15 additions & 8 deletions amazing/app/views/books/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
<article class="app-cont">
<article class="app-cont books-list">

<section class="app-title cf">
<h1>Books</h1>
<%= link_to 'New Book', new_book_path %>
<%= link_to 'Download (CSV)', books_path(:format => :csv) %>
</section>

<section class="app-content">
<div class="books-search">
<input type="text" id="books-search-txt" autofocus>
<div class="results" id="books-search-results"></div>
</div>
<% @books.each do |book| %>
<div class="book">
<p class="img">
<%= image_tag book.image_url %>
</p>
<h3 class="title"><%= book.title %></h3>
<h4 class="author">by <%= book.author %></h4>
<p class="details">
<span><%= number_to_currency(book.price) %></span>
<span>Published on <%= book.published_at.strftime('%B %Y') %></span>
</p>
</div>
<% end %>
</section>

</article>

<%= content_for :script do %>
new app.Books;
<% end %>
19 changes: 19 additions & 0 deletions amazing/app/views/books/search.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<article class="app-cont">

<section class="app-title cf">
<h1>Books</h1>
<%= link_to 'New Book', new_book_path %>
</section>

<section class="app-content">
<div class="books-search">
<input type="text" id="books-search-txt" autofocus>
<div class="results" id="books-search-results"></div>
</div>
</section>

</article>

<%= content_for :script do %>
new app.Books;
<% end %>
File renamed without changes.
8 changes: 8 additions & 0 deletions amazing/app/views/handlers/index.csv.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
csv << ['Title', 'Author']

@books.each do |book|
csv << [
book.title,
book.author
]
end
File renamed without changes.
8 changes: 6 additions & 2 deletions amazing/app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<header class="app-header">
<div class="inner app-cont">
<div class="inner app-cont cf">
<h1 class="logo">
<%= link_to 'amazing', root_path %>
<%= link_to 'amazing books', root_path %>
</h1>

<nav class="nav">
<li><%= link_to 'Search', search_books_path %></li>
</nav>
</div>
</header>
2 changes: 2 additions & 0 deletions amazing/config/initializers/handlers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ActionView::Template.register_template_handler :md, MarkdownHandler
ActionView::Template.register_template_handler :am, CsvHandler::Handler
1 change: 0 additions & 1 deletion amazing/config/initializers/markdown_handler.rb

This file was deleted.

1 change: 1 addition & 0 deletions amazing/config/initializers/mime_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf
# Mime::Type.register "text/csv", :csv
Loading

0 comments on commit 56ae941

Please sign in to comment.