Skip to content

Commit

Permalink
varios ajustes :-P
Browse files Browse the repository at this point in the history
  • Loading branch information
marciomoretto committed Mar 17, 2012
1 parent 4c7c4cf commit a8da00f
Show file tree
Hide file tree
Showing 28 changed files with 632 additions and 576 deletions.
7 changes: 1 addition & 6 deletions Gemfile
Expand Up @@ -26,12 +26,7 @@ group :assets do
gem 'sass-rails', " ~> 3.1.0"
gem 'coffee-rails', " ~> 3.1.0"
gem 'uglifier'
end

group :development do
gem 'rails3-generators'
gem 'nifty-generators'
gem 'pry'
gem 'twitter-bootstrap-rails', :git => 'git://github.com/marciomr/twitter-bootstrap-rails.git'
end

group :test do
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
@@ -1,5 +1,5 @@
class ApplicationController < ActionController::Base
helper_method :current_user, :admin?, :unauthorized!
helper_method :current_user, :admin
# See ActionController::RequestForgeryProtection for details
# Uncomment the :secret if you're not using the cookie session store
protect_from_forgery # :secret => '5f2f9061bb334aa869d4717f2779655e'
Expand Down
55 changes: 40 additions & 15 deletions app/controllers/books_controller.rb
Expand Up @@ -4,10 +4,21 @@ class BooksController < ApplicationController
load_and_authorize_resource
protect_from_forgery :only => [:create, :update, :destroy]

autocomplete :author, :name
autocomplete :tag, :title
for attribute in [:editor, :subject, :collection, :city, :country] do
autocomplete :book, attribute
%w(editor subject city country).each do |field|
define_method "typeahead_#{field}" do
@books = Book.order(field.to_sym).where("#{field} like ?", "%#{params[:query]}%")
render :json => @books.map(&(field.to_sym)).uniq
end
end

def typeahead_authors
@authors = Author.order(:name).where("name like ?", "%#{params[:query]}%")
render :json => @authors.map(&:name).uniq
end

def typeahead_tags
@tags = Tag.order(:title).where("title like ?", "%#{params[:query]}%")
render :json => @tags.map(&:title).uniq
end

def adv_search
Expand All @@ -18,8 +29,12 @@ def index
respond_to do |format|

format.html do
@adv_search = !params[:adv_search].nil?
@adv_search = params[:adv_search] if params[:adv_search]

if params[:book] && params[:user]
user = User.find_by_username(params[:user])
@book = user.books.find_by_tombo(params[:book])
end
@books = Book.search params[:search],
:star => true, # Automatic Wildcard
:field_weights => { # Order of relevance
Expand Down Expand Up @@ -66,27 +81,37 @@ def index

def show
@user = User.find_by_username(params[:user_id])
@book = @user.books.find_by_tombo(params[:id])
raise("not found") if @book.nil?
end

def new
@user = User.find_by_username(params[:user_id])
if (@user.try(:admin?))
redirect_to root_path, :alert => "O admin não pode possuir livros."
if @user != current_user
redirect_to new_user_book_path(current_user), :alert => "Acesso Negado!" if !current_user.admin?
end
if @user.nil?
redirect_to root_path, :alert => "Erro: você precisa estar no seu site para criar um livro."
else
@attributes = {}
@attributes = Book.get_attributes(params[:isbn]) if params[:isbn]
@book = Book.new
@book.authors.build

if params[:isbn]
@attributes = Book.get_attributes(params[:isbn]) || { 'isbn' => params[:isbn] }
@book = Book.new @attributes
else
@book = Book.new
end

@book.authors.build
end
end

def create
@book = Book.new(params[:book])
user = User.find_by_username(params[:user_id])
@book.user_id = user.try(:id)
@user = User.find_by_username(params[:user_id])
@book.user_id = @user.try(:id)

if @book.save
redirect_to user_book_path(user, @book), :notice => "Livro criado com sucesso."
redirect_to user_book_path(@user, @book), :notice => "Livro criado com sucesso."
else
render :action => 'new'
end
Expand All @@ -106,6 +131,6 @@ def update

def destroy
@book.destroy
redirect_to books_url, :notice => "Livro deletado com sucesso."
redirect_to root_path, :notice => "Livro deletado com sucesso." # queria voltar pra onde eu estava...
end
end
2 changes: 1 addition & 1 deletion app/helpers/application_helper.rb
@@ -1,7 +1,7 @@
module ApplicationHelper

def link_to_remove_fields(name, f)
f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)", :class => 'close')
end

def link_to_add_fields(name, f, association)
Expand Down
19 changes: 11 additions & 8 deletions app/models/book.rb
@@ -1,5 +1,5 @@
# coding: utf-8

require 'open-uri'
class Book < ActiveRecord::Base
extend FriendlyId
friendly_id :tombo
Expand Down Expand Up @@ -68,7 +68,7 @@ def self.fields
set_property :delta => true
end

['author', 'title', 'editor', 'language', 'collection'].each do |field|
%w(author title editor language collection).each do |field|
sphinx_scope("by_#{field}".to_sym) do | f |
{:conditions => { field.to_sym => f }}
end
Expand Down Expand Up @@ -142,7 +142,10 @@ def self.get_attributes_from_gbook(isbn)
attributes['subtitle'] = entry.css('dc|title').last.text.titleize
end

attributes['authors'] = entry.css("dc|creator").collect{ |a| a.text.titleize }
attributes['authors_attributes'] = {}
entry.css("dc|creator").each_with_index do |author, i|
attributes['authors_attributes'][i.to_s] = { 'name' => author.text.titleize }
end

attributes
end
Expand All @@ -151,16 +154,16 @@ def self.get_attributes_from_library(isbn)
book = Book.find_by_isbn(isbn)
if book
attributes = book.attributes
attributes['authors'] = Author.find_all_by_book_id(book.id).map(&:name)
attributes['authors_attributes'] = {}
Author.find_all_by_book_id(book.id).each_with_index do |author, i|
attributes['authors_attributes'][i.to_s] = { 'name' => author.name.titleize }
end
end
attributes
end

def self.get_attributes(isbn)
# attributes = Book.get_attributes_from_library(isbn)
# attributes ||= Book.get_attributes_from_gbook(isbn)
# attributes ||= {'isbn' => isbn}
Book.get_attributes_from_library(isbn) || Book.get_attributes_from_gbook(isbn) || { 'isbn' => isbn }
Book.get_attributes_from_library(isbn) || Book.get_attributes_from_gbook(isbn)
end


Expand Down
49 changes: 40 additions & 9 deletions app/views/books/_adv_search.html.erb
@@ -1,9 +1,40 @@
<div id="adv_search_content">
Título: <%= text_field_tag :title_filter, params[:title_filter], :size => 30 %><br/>
Autor: <%= text_field_tag :author_filter, params[:author_filter], :size => 30 %><br/>
Editor: <%= text_field_tag :editor_filter, params[:editor_filter], :size => 30 %><br/>
Língua: <%= select "language_filter", "", Book.languages, {:include_blank => true} %> <br/>
PDF: <%= check_box_tag :pdf_filter %><br/>

<%= link_to "esconder busca avançada", books_path, :remote => true, :id => :adv_search_hide %>
</div>
<fieldset>
<br/>
<legend>Busca Avançada</legend>
<%= form_tag books_path, :method => :get, :id => 'advanced_search' do %>
<div class="control-group">
Título
<div class="controls">
<%= text_field_tag :title_filter, params[:title_filter] %>
</div>
</div>
<div class="control-group">
Autor
<div class="controls">
<%= text_field_tag :author_filter, params[:author_filter] %>
</div>
</div>
<div class="control-group">
Editor
<div class="controls">
<%= text_field_tag :editor_filter, params[:editor_filter] %>
</div>
</div>
<div class="control-group">
Língua
<div class="controls">
<%= select "language_filter", "", Book.languages, {:include_blank => true} %>
</div>
</div>
<div class="control-group">
PDF
<div class="controls">
<%= check_box_tag :pdf_filter %>
</div>
</div>

<div class="form-actions">
<%= submit_tag "Busca", :class => 'btn btn-primary'%>
</div>
<% end %>
</fieldset>
16 changes: 7 additions & 9 deletions app/views/books/_author_fields.html.erb
@@ -1,9 +1,7 @@
<p class="fields">
<%= f.label :name, "Autor" %><br />
<% if name %>
<%= f.autocomplete_field :name, autocomplete_author_name_books_path, :class => "book_author", :value => name %>
<% else %>
<%= f.autocomplete_field :name, autocomplete_author_name_books_path, :class => "book_author"%>
<% end %>
<%= link_to_remove_fields "remove", f %>
</p>
<div class="control-group">
<%= f.label :name, "Autor", :class => 'control-label' %>
<div class="controls">
<%= f.text_field :name, :class => "book_authors text_field" %>
<span class="help-inline"><%= link_to_remove_fields raw("&times;"), f %></span>
</div>
</div>
135 changes: 0 additions & 135 deletions app/views/books/_form.html.erb

This file was deleted.

12 changes: 7 additions & 5 deletions app/views/books/_tag_fields.html.erb
@@ -1,5 +1,7 @@
<p class="fields">
<%= f.label :title, "Palavra Chave" %><br />
<%= f.autocomplete_field :title, autocomplete_tag_title_books_path %>
<%= link_to_remove_fields "remove", f %>
</p>
<div class="control-group">
<%= f.label :title, "Palavra Chave", :class => 'control-label' %>
<div class="controls">
<%= f.text_field :title, :class => "book_tags text_field" %>
<span class="help-inline"><%= link_to_remove_fields raw("&times;"), f %></span>
</div>
</div>

0 comments on commit a8da00f

Please sign in to comment.