ar_finder_form 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,82 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), 'spec_helper')
3
+
4
+ class OrderFinderForm2
5
+ include ArFinderForm
6
+
7
+ def initialize(attrs = {})
8
+ attrs.each{|key, value|send("#{key}=", value)}
9
+ end
10
+
11
+ with_model(Order) do
12
+ # joins
13
+ inner_join(:belongs_to => :user) do
14
+ # Duplicated column name, but each attr is different.
15
+ # like
16
+ column(:name, :attr => :user_name1)
17
+ # like forward
18
+ column(:name, :attr => :user_name2, :match => :forward)
19
+ # like backward
20
+ column(:name, :attr => :user_name3, :match => :backward)
21
+ end
22
+ end
23
+
24
+ find_options(:per_page => 50)
25
+ end
26
+
27
+
28
+ describe OrderFinderForm2 do
29
+
30
+ after do
31
+ Order.find(:all, @form.to_find_options)
32
+ @form.find(:all)
33
+ end
34
+
35
+ it "no attribute" do
36
+ @form = OrderFinderForm2.new
37
+ @form.to_find_options.should == {}
38
+ end
39
+
40
+ it "user_name1" do
41
+ @form = OrderFinderForm2.new(:user_name1 => 'ABC')
42
+ @form.to_find_options.should == {
43
+ :conditions => ["cond_users.name LIKE ?", '%ABC%'],
44
+ :joins => "INNER JOIN users cond_users ON cond_users.id = orders.user_id"
45
+ }
46
+ @form.to_paginate_options(:page => '2').should ==
47
+ {:per_page => 50, :page => '2'}.update(@form.to_find_options)
48
+ end
49
+
50
+ it "user_name2" do
51
+ @form = OrderFinderForm2.new(:user_name2 => 'ABC')
52
+ @form.to_find_options.should == {
53
+ :conditions => ["cond_users.name LIKE ?", 'ABC%'],
54
+ :joins => "INNER JOIN users cond_users ON cond_users.id = orders.user_id"
55
+ }
56
+ @form.to_paginate_options(:page => nil).should ==
57
+ {:per_page => 50}.update(@form.to_find_options)
58
+ end
59
+
60
+ it "user_name3" do
61
+ @form = OrderFinderForm2.new(:user_name3 => 'ABC')
62
+ @form.to_find_options.should == {
63
+ :conditions => ["cond_users.name LIKE ?", '%ABC'],
64
+ :joins => "INNER JOIN users cond_users ON cond_users.id = orders.user_id"
65
+ }
66
+ @form.to_paginate_options(:page => 3).should ==
67
+ {:per_page => 50, :page => 3}.update(@form.to_find_options)
68
+ end
69
+
70
+ describe "pagenate" do
71
+ it "valid" do
72
+ Order.should_receive(:paginate).with({
73
+ :per_page => 50, :page => 4,
74
+ :conditions => ["cond_users.name LIKE ?", '%ABC'],
75
+ :joins => "INNER JOIN users cond_users ON cond_users.id = orders.user_id"
76
+ })
77
+ @form = OrderFinderForm2.new(:user_name3 => 'ABC')
78
+ @form.paginate(:page => 4)
79
+ end
80
+ end
81
+
82
+ end
@@ -0,0 +1,5 @@
1
+ # -*- coding: utf-8 -*-
2
+ class Order < ActiveRecord::Base
3
+ belongs_to :user
4
+ belongs_to :product
5
+ end
@@ -0,0 +1,11 @@
1
+ # -*- coding: utf-8 -*-
2
+ class Product < ActiveRecord::Base
3
+ has_many :orders
4
+
5
+ selectable_attr :category_cd do
6
+ entry '01', :book, '書籍'
7
+ entry '02', :food, '食品'
8
+ entry '03', :toy, 'おもちゃ'
9
+ end
10
+
11
+ end
@@ -0,0 +1,4 @@
1
+ # -*- coding: utf-8 -*-
2
+ class User < ActiveRecord::Base
3
+ has_many :orders
4
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,30 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ # Users are created and updated by other Users
3
+ create_table :products, :force => true do |t|
4
+ t.string :category_cd
5
+ t.string :code
6
+ t.string :name
7
+ t.float :price
8
+ t.integer :stock
9
+ t.time :released_at
10
+ t.timestamp
11
+ end
12
+
13
+ create_table :orders, :force => true do |t|
14
+ t.integer :user_id
15
+ t.integer :product_id
16
+ t.integer :amount
17
+ t.float :price
18
+ t.date :delivery_estimate
19
+ t.time :delivered_at
20
+ t.time :deleted_at
21
+ t.timestamp
22
+ end
23
+
24
+ create_table :users, :force => true do |t|
25
+ t.string :login
26
+ t.string :name
27
+ t.timestamp
28
+ end
29
+
30
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,6 @@
1
+ --colour
2
+ --format
3
+ progress
4
+ --loadby
5
+ mtime
6
+ --reverse
@@ -0,0 +1,83 @@
1
+ # -*- coding: utf-8 -*-
2
+ $KCODE='u'
3
+
4
+ ENV['RAILS_ENV'] ||= 'test'
5
+ unless defined?(RAILS_ENV)
6
+ RAILS_ENV = 'test'
7
+ RAILS_ROOT = File.dirname(__FILE__) unless defined?(RAILS_ROOT)
8
+
9
+ require 'rubygems'
10
+ require 'spec'
11
+ require 'spec/matchers'
12
+
13
+ require 'active_support'
14
+ require 'active_record'
15
+ require 'active_record/fixtures'
16
+ # require 'action_mailer'
17
+ # require 'action_controller'
18
+ # require 'action_view'
19
+ require 'initializer'
20
+
21
+ require 'yaml'
22
+ config = YAML.load(IO.read(File.join(File.dirname(__FILE__), 'database.yml')))
23
+
24
+ ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite3'])
25
+
26
+ load(File.join(File.dirname(__FILE__), 'schema.rb'))
27
+
28
+ # ActionController::Routing::Routes.draw do |map|
29
+ # map.connect ':controller/:action/:id.:format'
30
+ # map.connect ':controller/:action/:id'
31
+ # end
32
+
33
+ gem 'selectable_attr' , ">=0.3.7"
34
+ gem 'selectable_attr_rails', ">=0.3.7"
35
+ require 'selectable_attr'
36
+ require 'selectable_attr_i18n'
37
+ require 'selectable_attr_rails'
38
+ SelectableAttrRails.add_features_to_active_record
39
+
40
+
41
+ # %w(resources/models resources/controllers).each do |path|
42
+ %w(resources/models).each do |path|
43
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), path)
44
+ ActiveSupport::Dependencies.load_paths << File.join(File.dirname(__FILE__), path)
45
+ end
46
+
47
+ require 'spec/autorun'
48
+ # require 'spec/rails'
49
+
50
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
51
+ require File.join(File.dirname(__FILE__), '..', 'init')
52
+
53
+ Dir.glob("resources/**/*.rb") do |filename|
54
+ require filename
55
+ end
56
+
57
+ class ActiveSupport::TestCase
58
+ include ActiveRecord::TestFixtures
59
+ self.fixture_path = File.join(File.dirname(__FILE__), 'fixtures')
60
+ self.use_transactional_fixtures = false
61
+ self.use_instantiated_fixtures = false
62
+ self.pre_loaded_fixtures = false
63
+ fixtures :all
64
+
65
+ def setup_fixtures_with_set_fixture_path
66
+ # ここでなぜか fixture_path の値が変わってしまっています。。。
67
+ self.class.fixture_path = File.join(File.dirname(__FILE__), 'fixtures')
68
+ setup_fixtures_without_set_fixture_path
69
+ end
70
+ alias_method_chain :setup_fixtures, :set_fixture_path
71
+ end
72
+
73
+ ActiveRecord::Base.configurations = true
74
+
75
+ Spec::Matchers.define :be_ar_column do |name|
76
+ match do |obj|
77
+ obj.class.should <= ActiveRecord::ConnectionAdapters::Column
78
+ obj.name.should == name.to_s
79
+ end
80
+ end
81
+ end
82
+
83
+
@@ -0,0 +1,48 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), 'spec_helper')
3
+
4
+ class OrderFinderFormForTable
5
+ include ArFinderForm
6
+
7
+ with_model(Order) do
8
+ end
9
+
10
+ end
11
+
12
+
13
+ describe ArFinderForm::Table do
14
+
15
+ before do
16
+ @table = OrderFinderFormForTable.builder
17
+ end
18
+
19
+ it "name" do
20
+ @table.name.should == "orders"
21
+ end
22
+
23
+ it "root_table" do
24
+ @table.root_table.should == @table
25
+ end
26
+
27
+ describe "model_column_for" do
28
+ column_names = [:id, :user_id, :product_id, :amount, :price,
29
+ :delivery_estimate, :delivered_at, :deleted_at]
30
+
31
+ it "by Symbol" do
32
+ column_names.each do |column_name|
33
+ @table.model_column_for(column_name).should be_ar_column(column_name.to_s)
34
+ end
35
+ @table.model_column_for('unexist_column').should be_nil
36
+ end
37
+
38
+ it "by String" do
39
+ column_names.each do |column_name|
40
+ @table.model_column_for(column_name.to_s).should be_ar_column(column_name.to_s)
41
+ end
42
+ @table.model_column_for(:unexist_column).should be_nil
43
+ end
44
+
45
+ end
46
+
47
+
48
+ end
@@ -0,0 +1,94 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), 'spec_helper')
3
+
4
+ class UserFinderForm1
5
+ include ArFinderForm
6
+
7
+ def initialize(attrs = {})
8
+ attrs.each{|key, value|send("#{key}=", value)}
9
+ end
10
+
11
+ with_model(User) do
12
+ column(:name, :attr => :user_name)
13
+ inner_join(:has_many => :orders) do
14
+ column(:product_id, :attr => :product_ids, :operator => :IN)
15
+ inner_join(:belongs_to => :product) do
16
+ column(:name, :attr => :product_name)
17
+ column(:price)
18
+ end
19
+ end
20
+ end
21
+
22
+ find_options(:order => "name asc")
23
+
24
+ attr_accessor :order_expression
25
+ def before_build(context)
26
+ context.find_options[:order] = order_expression if order_expression
27
+ end
28
+
29
+ def after_build(context)
30
+ if context.joins.any?{|join| join =~ /cond_products/}
31
+ context.find_options[:order] = "#{context.find_options[:order]}, cond_products.code asc"
32
+ end
33
+ end
34
+ end
35
+
36
+
37
+ describe UserFinderForm1 do
38
+
39
+ after do
40
+ User.find(:all, @form.to_find_options)
41
+ end
42
+
43
+ it "no attribute" do
44
+ @form = UserFinderForm1.new
45
+ @form.to_find_options.should == {:order => "name asc"}
46
+ end
47
+
48
+ it "user_name" do
49
+ @form = UserFinderForm1.new(:user_name => 'ABC')
50
+ @form.to_find_options.should == {
51
+ :order => "name asc",
52
+ :conditions => ["users.name LIKE ?", '%ABC%']
53
+ }
54
+ end
55
+
56
+ it "product_ids" do
57
+ @form = UserFinderForm1.new(:product_ids => %w(1 3 7))
58
+ @form.to_find_options.should == {
59
+ :order => "name asc",
60
+ :conditions => ["cond_orders.product_id IN (?)", [1,3,7]],
61
+ :joins => "INNER JOIN orders cond_orders ON cond_orders.user_id = users.id"
62
+ }
63
+ end
64
+
65
+ it "product_ids with custom order" do
66
+ @form = UserFinderForm1.new(:product_ids => %w(1 3 7), :order_expression => "login desc")
67
+ @form.to_find_options.should == {
68
+ :order => "login desc",
69
+ :conditions => ["cond_orders.product_id IN (?)", [1,3,7]],
70
+ :joins => "INNER JOIN orders cond_orders ON cond_orders.user_id = users.id"
71
+ }
72
+ end
73
+
74
+ it "product_name" do
75
+ @form = UserFinderForm1.new(:product_name => "PPP")
76
+ @form.to_find_options.should == {
77
+ :order => "name asc, cond_products.code asc",
78
+ :conditions => ["cond_products.name LIKE ?", "%PPP%"],
79
+ :joins => "INNER JOIN orders cond_orders ON cond_orders.user_id = users.id" <<
80
+ " INNER JOIN products cond_products ON cond_products.id = cond_orders.product_id"
81
+ }
82
+ end
83
+
84
+ it "product_name" do
85
+ @form = UserFinderForm1.new(:product_name => "PPP")
86
+ @form.to_find_options.should == {
87
+ :order => "name asc, cond_products.code asc",
88
+ :conditions => ["cond_products.name LIKE ?", "%PPP%"],
89
+ :joins => "INNER JOIN orders cond_orders ON cond_orders.user_id = users.id" <<
90
+ " INNER JOIN products cond_products ON cond_products.id = cond_orders.product_id"
91
+ }
92
+ end
93
+
94
+ end
@@ -0,0 +1,66 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), 'spec_helper')
3
+
4
+ class OrderFinderForm3
5
+ include ArFinderForm
6
+ include SelectableAttr::Base
7
+
8
+ def initialize(attrs = {})
9
+ attrs.each{|key, value|send("#{key}=", value)}
10
+ end
11
+
12
+ with_model(Order) do
13
+ column(:price)
14
+ column(:amount)
15
+
16
+ inner_join(:belongs_to => :product) do
17
+ column(:name, :attr => :product_name)
18
+ column(:code, :attr => :product_code)
19
+ end
20
+ end
21
+
22
+ selectable_attr :order_cd, :default => '1' do
23
+ entry '1', :price, "金額" , :order => 'orders.price desc'
24
+ entry '2', :amount, "数量", :order => 'orders.amount desc'
25
+ entry '3', :product_name, "商品名" , :order => 'products.name asc', :include => :product
26
+ entry '4', :product_code, "商品コード", :order => 'products.code asc', :include => :product
27
+ end
28
+
29
+ selectable_attr :per_page_count, :default => '10' do
30
+ [10, 20, 50, 100].each do |cnt|
31
+ entry cnt.to_s, cnt.to_s.to_sym, "#{cnt} records"
32
+ end
33
+ end
34
+
35
+ def after_build(context)
36
+ context.paginate_options[:per_page] = self.per_page_count.to_i
37
+ return if self.order_entry.null?
38
+ context.find_options.update(self.order_entry.instance_variable_get(:@options) || {})
39
+ end
40
+
41
+ end
42
+
43
+ describe OrderFinderForm3 do
44
+ it "default" do
45
+ @form = OrderFinderForm3.new
46
+ @form.to_find_options.should == {:order => "orders.price desc"}
47
+ @form.to_paginate_options(:page => 5).should == {
48
+ :per_page => 10, :page => 5
49
+ }.update(@form.to_find_options)
50
+ end
51
+
52
+ it "default" do
53
+ @form = OrderFinderForm3.new(:product_name => "ABC", :order_key => :product_name, :per_page_count => '50')
54
+ @form.to_find_options.should == {
55
+ :order => "products.name asc",
56
+ :conditions => ["cond_products.name LIKE ?", '%ABC%'],
57
+ :joins=>"INNER JOIN products cond_products ON cond_products.id = orders.product_id",
58
+ :include => :product
59
+ }
60
+ @form.to_paginate_options(:page => 6).should == {
61
+ :per_page => 50, :page => 6
62
+ }.update(@form.to_find_options)
63
+ end
64
+
65
+ end
66
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :finder_form do
3
+ # # Task goes here
4
+ # end