Skip to content

Commit

Permalink
fixed associations and debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Berkovich committed Oct 1, 2011
1 parent 7f5ea47 commit 7d1c5c8
Show file tree
Hide file tree
Showing 23 changed files with 153 additions and 13 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
== 3.1.2, released 2011-10-01
* fixed associations
* fixed debugging info

== 3.1.1, released 2011-09-29
== 0.1.0, released 2010-07-02
* upgraded to Rails 3.1

== 2.3.1, released 2010-07-02
21 changes: 14 additions & 7 deletions app/models/will_filter/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,8 @@ def definition
model_columns.each do |col|
defs[col.name.to_sym] = default_condition_definition_for(col.name, col.sql_type)
end

inner_joins.each do |inner_join|
join_class = inner_join.first.to_s.camelcase.constantize
join_class = association_class(inner_join)
join_class.columns.each do |col|
defs[:"#{join_class.to_s.underscore}.#{col.name.to_sym}"] = default_condition_definition_for(col.name, col.sql_type)
end
Expand Down Expand Up @@ -238,7 +237,7 @@ def order_type_options
# Can be overloaded for custom titles
#############################################################################
def condition_title_for(key)
title = key.to_s.gsub(".", ": ").gsub("_", " ")
title = key.to_s.gsub(".", ": ").gsub("_", " ").split("/").last
title.split(" ").collect{|part| part.capitalize}.join(" ")
end

Expand Down Expand Up @@ -531,7 +530,7 @@ def debug_conditions(conds)

all_conditions << cond
end
all_conditions
all_conditions.join("")
end

def debug_sql_conditions
Expand Down Expand Up @@ -666,12 +665,20 @@ def custom_formats
def process_custom_format
""
end

def association_name(inner_join)
(inner_join.is_a?(Array) ? inner_join.first : inner_join).to_sym
end

def association_class(inner_join)
model_class.new.association(association_name(inner_join)).build.class
end

# deprecated for Rails 3.0 and up
def joins
return nil if inner_joins.empty?
inner_joins.collect do |inner_join|
join_table_name = inner_join.first.to_s.camelcase.constantize.table_name
join_table_name = association_class(inner_join).table_name
join_on_field = inner_join.last.to_s
"INNER JOIN #{join_table_name} ON #{join_table_name}.id = #{table_name}.#{join_on_field}"
end
Expand All @@ -681,8 +688,8 @@ def results
@results ||= begin
handle_empty_filter!
recs = model_class.where(sql_conditions).order(order_clause)
inner_joins.each do |j|
recs = recs.joins(j.first)
inner_joins.each do |inner_join|
recs = recs.joins(association_name(inner_join))
end
recs = recs.page(page).per(per_page)
recs.wf_filter = self
Expand Down
2 changes: 1 addition & 1 deletion app/views/will_filter/filter/_container.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</div>

<div id="wf_debugger" class="debugger" style="display:none;">
<%= wf_filter.debug_sql_conditions %>
<%= raw(wf_filter.debug_sql_conditions) %>
</div>
</div>
<% end %>
2 changes: 1 addition & 1 deletion lib/will_filter/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
#++

module WillFilter
VERSION = "3.1.1"
VERSION = "3.1.2"
end
2 changes: 2 additions & 0 deletions test/dummy/app/assets/javascripts/orders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
4 changes: 4 additions & 0 deletions test/dummy/app/assets/stylesheets/orders.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Place all the styles related to the matching controller here.
They will automatically be included in application.css.
*/
9 changes: 9 additions & 0 deletions test/dummy/app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class OrdersController < ApplicationController
def index
@orders = Merchant::Order.filter(:params => params, :filter => Merchant::OrderFilter)
end

def items
@order_items = Merchant::OrderItem.filter(:params => params, :filter => Merchant::OrderItemFilter)
end
end
2 changes: 2 additions & 0 deletions test/dummy/app/helpers/orders_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module OrdersHelper
end
2 changes: 1 addition & 1 deletion test/dummy/app/models/event_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def default_filter_conditions(key)
end

def inner_joins
[[:user, :creator_id]]
[:user]
end

end
2 changes: 1 addition & 1 deletion test/dummy/app/models/event_user_filter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class EventUserFilter < WillFilter::Filter

def inner_joins
[[:user, :user_id], [:event, :event_id]]
[:user, :event]
end

def definition
Expand Down
7 changes: 7 additions & 0 deletions test/dummy/app/models/merchant/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Merchant
class Order < ActiveRecord::Base
set_table_name :merchant_orders
belongs_to :user
has_many :order_items
end
end
9 changes: 9 additions & 0 deletions test/dummy/app/models/merchant/order_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Merchant
class OrderFilter < WillFilter::Filter

def inner_joins
[:user]
end

end
end
6 changes: 6 additions & 0 deletions test/dummy/app/models/merchant/order_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Merchant
class OrderItem < ActiveRecord::Base
set_table_name :merchant_order_items
belongs_to :order, :class_name => "Merchant::Order"
end
end
9 changes: 9 additions & 0 deletions test/dummy/app/models/merchant/order_item_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Merchant
class OrderItemFilter < WillFilter::Filter

def inner_joins
[:order]
end

end
end
16 changes: 16 additions & 0 deletions test/dummy/app/views/layouts/orders.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>will_filter examples</title>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body style="margin:0px;">
<center>
<div style="margin-top:20px;width:900px;text-align:left;">
<%= yield %>
</div>
</center>
</body>
</html>
2 changes: 2 additions & 0 deletions test/dummy/app/views/orders/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%= will_filter_tag(@orders) %>
<%= will_filter_table_tag(@orders) %>
2 changes: 2 additions & 0 deletions test/dummy/app/views/orders/items.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%= will_filter_tag(@order_items) %>
<%= will_filter_table_tag(@order_items) %>
4 changes: 4 additions & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do

mount WillFilter::Engine => "/will_filter"

match 'simple/users'
Expand All @@ -8,6 +9,9 @@
match 'advanced/users_with_actions'
match 'advanced/events'
match 'advanced/event_members'

match 'orders/index', :to => "orders#index"
match 'orders/items'

root :to => "simple#users"
end
13 changes: 13 additions & 0 deletions test/dummy/db/migrate/20111001193019_create_orders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateOrders < ActiveRecord::Migration
def self.up
create_table :merchant_orders do |t|
t.integer :user_id
t.integer :amount
t.timestamps
end
end

def self.down
drop_table :merchant_orders
end
end
14 changes: 14 additions & 0 deletions test/dummy/db/migrate/20111001193027_create_order_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateOrderItems < ActiveRecord::Migration
def self.up
create_table :merchant_order_items do |t|
t.integer :order_id
t.string :name
t.integer :price
t.timestamps
end
end

def self.down
drop_table :merchant_order_items
end
end
17 changes: 16 additions & 1 deletion test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20110924023807) do
ActiveRecord::Schema.define(:version => 20111001193027) do

create_table "event_users", :force => true do |t|
t.integer "event_id"
Expand All @@ -37,6 +37,21 @@

add_index "events", ["creator_id"], :name => "index_events_on_creator_id"

create_table "merchant_order_items", :force => true do |t|
t.integer "order_id"
t.string "name"
t.integer "price"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "merchant_orders", :force => true do |t|
t.integer "user_id"
t.integer "amount"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "users", :force => true do |t|
t.string "first_name"
t.string "last_name"
Expand Down
9 changes: 9 additions & 0 deletions test/dummy/test/functional/orders_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'test_helper'

class OrdersControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
end

end
4 changes: 4 additions & 0 deletions test/dummy/test/unit/helpers/orders_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'test_helper'

class OrdersHelperTest < ActionView::TestCase
end

0 comments on commit 7d1c5c8

Please sign in to comment.