diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index d7129eb..ce2315f 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -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 diff --git a/app/models/will_filter/filter.rb b/app/models/will_filter/filter.rb index 2351048..9fc1d8a 100644 --- a/app/models/will_filter/filter.rb +++ b/app/models/will_filter/filter.rb @@ -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 @@ -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 @@ -531,7 +530,7 @@ def debug_conditions(conds) all_conditions << cond end - all_conditions + all_conditions.join("") end def debug_sql_conditions @@ -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 @@ -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 diff --git a/app/views/will_filter/filter/_container.html.erb b/app/views/will_filter/filter/_container.html.erb index f801330..17debd9 100755 --- a/app/views/will_filter/filter/_container.html.erb +++ b/app/views/will_filter/filter/_container.html.erb @@ -25,7 +25,7 @@ <% end %> diff --git a/lib/will_filter/version.rb b/lib/will_filter/version.rb index 8b57f7b..09467b9 100644 --- a/lib/will_filter/version.rb +++ b/lib/will_filter/version.rb @@ -22,5 +22,5 @@ #++ module WillFilter - VERSION = "3.1.1" + VERSION = "3.1.2" end diff --git a/test/dummy/app/assets/javascripts/orders.js b/test/dummy/app/assets/javascripts/orders.js new file mode 100644 index 0000000..dee720f --- /dev/null +++ b/test/dummy/app/assets/javascripts/orders.js @@ -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. diff --git a/test/dummy/app/assets/stylesheets/orders.css b/test/dummy/app/assets/stylesheets/orders.css new file mode 100644 index 0000000..afad32d --- /dev/null +++ b/test/dummy/app/assets/stylesheets/orders.css @@ -0,0 +1,4 @@ +/* + Place all the styles related to the matching controller here. + They will automatically be included in application.css. +*/ diff --git a/test/dummy/app/controllers/orders_controller.rb b/test/dummy/app/controllers/orders_controller.rb new file mode 100644 index 0000000..36d354c --- /dev/null +++ b/test/dummy/app/controllers/orders_controller.rb @@ -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 diff --git a/test/dummy/app/helpers/orders_helper.rb b/test/dummy/app/helpers/orders_helper.rb new file mode 100644 index 0000000..443227f --- /dev/null +++ b/test/dummy/app/helpers/orders_helper.rb @@ -0,0 +1,2 @@ +module OrdersHelper +end diff --git a/test/dummy/app/models/event_filter.rb b/test/dummy/app/models/event_filter.rb index 49c37e1..5617e07 100755 --- a/test/dummy/app/models/event_filter.rb +++ b/test/dummy/app/models/event_filter.rb @@ -35,7 +35,7 @@ def default_filter_conditions(key) end def inner_joins - [[:user, :creator_id]] + [:user] end end diff --git a/test/dummy/app/models/event_user_filter.rb b/test/dummy/app/models/event_user_filter.rb index 835af53..40ca862 100755 --- a/test/dummy/app/models/event_user_filter.rb +++ b/test/dummy/app/models/event_user_filter.rb @@ -1,7 +1,7 @@ class EventUserFilter < WillFilter::Filter def inner_joins - [[:user, :user_id], [:event, :event_id]] + [:user, :event] end def definition diff --git a/test/dummy/app/models/merchant/order.rb b/test/dummy/app/models/merchant/order.rb new file mode 100644 index 0000000..e386c8b --- /dev/null +++ b/test/dummy/app/models/merchant/order.rb @@ -0,0 +1,7 @@ +module Merchant + class Order < ActiveRecord::Base + set_table_name :merchant_orders + belongs_to :user + has_many :order_items + end +end \ No newline at end of file diff --git a/test/dummy/app/models/merchant/order_filter.rb b/test/dummy/app/models/merchant/order_filter.rb new file mode 100644 index 0000000..6e64cbd --- /dev/null +++ b/test/dummy/app/models/merchant/order_filter.rb @@ -0,0 +1,9 @@ +module Merchant + class OrderFilter < WillFilter::Filter + + def inner_joins + [:user] + end + + end +end \ No newline at end of file diff --git a/test/dummy/app/models/merchant/order_item.rb b/test/dummy/app/models/merchant/order_item.rb new file mode 100644 index 0000000..20daf35 --- /dev/null +++ b/test/dummy/app/models/merchant/order_item.rb @@ -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 \ No newline at end of file diff --git a/test/dummy/app/models/merchant/order_item_filter.rb b/test/dummy/app/models/merchant/order_item_filter.rb new file mode 100644 index 0000000..9d7652e --- /dev/null +++ b/test/dummy/app/models/merchant/order_item_filter.rb @@ -0,0 +1,9 @@ +module Merchant + class OrderItemFilter < WillFilter::Filter + + def inner_joins + [:order] + end + + end +end \ No newline at end of file diff --git a/test/dummy/app/views/layouts/orders.html.erb b/test/dummy/app/views/layouts/orders.html.erb new file mode 100644 index 0000000..256f29f --- /dev/null +++ b/test/dummy/app/views/layouts/orders.html.erb @@ -0,0 +1,16 @@ + + + + will_filter examples + <%= stylesheet_link_tag "application" %> + <%= javascript_include_tag "application" %> + <%= csrf_meta_tags %> + + +
+
+ <%= yield %> +
+
+ + diff --git a/test/dummy/app/views/orders/index.html.erb b/test/dummy/app/views/orders/index.html.erb new file mode 100644 index 0000000..f10044f --- /dev/null +++ b/test/dummy/app/views/orders/index.html.erb @@ -0,0 +1,2 @@ +<%= will_filter_tag(@orders) %> +<%= will_filter_table_tag(@orders) %> diff --git a/test/dummy/app/views/orders/items.html.erb b/test/dummy/app/views/orders/items.html.erb new file mode 100644 index 0000000..ec2a081 --- /dev/null +++ b/test/dummy/app/views/orders/items.html.erb @@ -0,0 +1,2 @@ +<%= will_filter_tag(@order_items) %> +<%= will_filter_table_tag(@order_items) %> diff --git a/test/dummy/config/routes.rb b/test/dummy/config/routes.rb index 5a8735b..ac27e11 100644 --- a/test/dummy/config/routes.rb +++ b/test/dummy/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + mount WillFilter::Engine => "/will_filter" match 'simple/users' @@ -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 diff --git a/test/dummy/db/migrate/20111001193019_create_orders.rb b/test/dummy/db/migrate/20111001193019_create_orders.rb new file mode 100644 index 0000000..557650a --- /dev/null +++ b/test/dummy/db/migrate/20111001193019_create_orders.rb @@ -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 diff --git a/test/dummy/db/migrate/20111001193027_create_order_items.rb b/test/dummy/db/migrate/20111001193027_create_order_items.rb new file mode 100644 index 0000000..aabeda1 --- /dev/null +++ b/test/dummy/db/migrate/20111001193027_create_order_items.rb @@ -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 diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index aa0ddf9..16190d9 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb @@ -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" @@ -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" diff --git a/test/dummy/test/functional/orders_controller_test.rb b/test/dummy/test/functional/orders_controller_test.rb new file mode 100644 index 0000000..fc61a3b --- /dev/null +++ b/test/dummy/test/functional/orders_controller_test.rb @@ -0,0 +1,9 @@ +require 'test_helper' + +class OrdersControllerTest < ActionController::TestCase + test "should get index" do + get :index + assert_response :success + end + +end diff --git a/test/dummy/test/unit/helpers/orders_helper_test.rb b/test/dummy/test/unit/helpers/orders_helper_test.rb new file mode 100644 index 0000000..52dacae --- /dev/null +++ b/test/dummy/test/unit/helpers/orders_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class OrdersHelperTest < ActionView::TestCase +end