Skip to content

Commit

Permalink
fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
scambra committed Jul 12, 2013
1 parent 76f7d2a commit fb38fab
Show file tree
Hide file tree
Showing 29 changed files with 152 additions and 173 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require 'find'

desc 'Test ActiveScaffold.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'lib' << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
Expand Down
4 changes: 2 additions & 2 deletions lib/active_scaffold/attribute_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ def column_value_from_param_simple_value(parent_record, column, value)
end
elsif column.plural_association?
column_plural_assocation_value_from_value(column, Array(value))
elsif column.number? && [:i18n_number, :currency].include?(column.options[:format]) && column.form_ui != :number
self.class.i18n_number_to_native_format(value)
elsif column.number? && column.options[:format] && column.form_ui != :number
column.number_to_native(value)
else
# convert empty strings into nil. this works better with 'null => true' columns (and validations),
# and 'null => false' columns should just convert back to an empty string.
Expand Down
2 changes: 0 additions & 2 deletions lib/active_scaffold/config/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,6 @@ def initialize(model_id)

# To be called after your finished configuration
def _load_action_columns
#ActiveScaffold::DataStructures::ActionColumns.class_eval {include ActiveScaffold::DataStructures::ActionColumns::AfterConfiguration}

# then, register the column objects
self.actions.each do |action_name|
action = self.send(action_name)
Expand Down
4 changes: 2 additions & 2 deletions lib/active_scaffold/constraints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def conditions_from_constraints
elsif column.association
if column.association.macro == :has_and_belongs_to_many
active_scaffold_habtm_joins.concat column.includes
else
elsif !column.association.options[:polymorphic]
active_scaffold_includes.concat column.includes
end
hash_conditions.merge!(condition_from_association_constraint(column.association, v))
Expand Down Expand Up @@ -116,7 +116,7 @@ def condition_from_association_constraint(association, value)
condition = {"#{table}.#{field}" => value}
if association.options[:polymorphic]
raise ActiveScaffold::MalformedConstraint, polymorphic_constraint_error(association), caller unless params[:parent_model]
condition["#{table}.#{association.name}_type"] = params[:parent_model].constantize.model.to_s
condition["#{table}.#{association.name}_type"] = params[:parent_model].constantize.to_s
end

condition
Expand Down
140 changes: 68 additions & 72 deletions lib/active_scaffold/data_structures/action_columns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,88 +59,84 @@ def names_without_auth_check
Array(@set)
end

# A package of stuff to add after the configuration block. This is an attempt at making a certain level of functionality inaccessible during configuration, to reduce possible breakage from misuse.
# The bulk of the package is a means of connecting the referential column set (ActionColumns) with the actual column objects (Columns). This lets us iterate over the set and yield real column objects.
#module AfterConfiguration
# Redefine the each method to yield actual Column objects.
# It will skip constrained and unauthorized columns.
#
# Options:
# * :flatten - whether to recursively iterate on nested sets. default is false.
# * :for - the record (or class) being iterated over. used for column-level security. default is the class.
def each(options = {}, &proc)
options[:for] ||= @columns.active_record_class unless @columns.nil?
self.unauthorized_columns = []
@set.each do |item|
unless item.is_a?(ActiveScaffold::DataStructures::ActionColumns) || @columns.nil?
item = (@columns[item] || ActiveScaffold::DataStructures::Column.new(item.to_sym, @columns.active_record_class))
next if self.skip_column?(item, options)
end
if item.is_a? ActiveScaffold::DataStructures::ActionColumns
if options[:flatten]
item.each(options, &proc)
elsif !options[:skip_groups]
yield item
end
else
# Redefine the each method to yield actual Column objects.
# It will skip constrained and unauthorized columns.
#
# Options:
# * :flatten - whether to recursively iterate on nested sets. default is false.
# * :for - the record (or class) being iterated over. used for column-level security. default is the class.
def each(options = {}, &proc)
options[:for] ||= @columns.active_record_class unless @columns.nil?
self.unauthorized_columns = []
@set.each do |item|
unless item.is_a?(ActiveScaffold::DataStructures::ActionColumns) || @columns.nil?
item = (@columns[item] || ActiveScaffold::DataStructures::Column.new(item.to_sym, @columns.active_record_class))
next if self.skip_column?(item, options)
end
if item.is_a? ActiveScaffold::DataStructures::ActionColumns
if options[:flatten]
item.each(options, &proc)
elsif !options[:skip_groups]
yield item
end
else
yield item
end
end

def collect_visible(options = {}, &proc)
columns = []
options[:for] ||= @columns.active_record_class
self.unauthorized_columns = []
@set.each do |item|
unless item.is_a? ActiveScaffold::DataStructures::ActionColumns || @columns.nil?
item = (@columns[item] || ActiveScaffold::DataStructures::Column.new(item.to_sym, @columns.active_record_class))
next if self.skip_column?(item, options)
end
if item.is_a? ActiveScaffold::DataStructures::ActionColumns and options.has_key?(:flatten) and options[:flatten]
columns += item.collect_visible(options, &proc)
else
columns << (block_given? ? yield(item) : item)
end
end

def collect_visible(options = {}, &proc)
columns = []
options[:for] ||= @columns.active_record_class
self.unauthorized_columns = []
@set.each do |item|
unless item.is_a? ActiveScaffold::DataStructures::ActionColumns || @columns.nil?
item = (@columns[item] || ActiveScaffold::DataStructures::Column.new(item.to_sym, @columns.active_record_class))
next if self.skip_column?(item, options)
end
columns
end

def skip_column?(column, options)
result = false
# skip if this matches a constrained column
result = true if constraint_columns.include?(column.name.to_sym)
# skip this field if it's not authorized
unless options[:for].authorized_for?(:action => options[:action], :crud_type => options[:crud_type] || self.action.try(:crud_type), :column => column.name)
self.unauthorized_columns << column.name.to_sym
result = true
if item.is_a? ActiveScaffold::DataStructures::ActionColumns and options.has_key?(:flatten) and options[:flatten]
columns += item.collect_visible(options, &proc)
else
columns << (block_given? ? yield(item) : item)
end
return result
end

# registers a set of column objects (recursively, for all nested ActionColumns)
def set_columns(columns)
@columns = columns
# iterate over @set instead of self to avoid dealing with security queries
@set.each do |item|
item.set_columns(columns) if item.respond_to? :set_columns
end
columns
end

def skip_column?(column, options)
result = false
# skip if this matches a constrained column
result = true if constraint_columns.include?(column.name.to_sym)
# skip this field if it's not authorized
unless options[:for].authorized_for?(:action => options[:action], :crud_type => options[:crud_type] || self.action.try(:crud_type), :column => column.name)
self.unauthorized_columns << column.name.to_sym
result = true
end
return result
end

attr_writer :constraint_columns
def constraint_columns
@constraint_columns ||= []
# registers a set of column objects (recursively, for all nested ActionColumns)
def set_columns(columns)
@columns = columns
# iterate over @set instead of self to avoid dealing with security queries
@set.each do |item|
item.set_columns(columns) if item.respond_to? :set_columns
end

attr_writer :unauthorized_columns
def unauthorized_columns
@unauthorized_columns ||= []
end

def length
((@set - self.constraint_columns) - self.unauthorized_columns).length
end
#end
end

attr_writer :constraint_columns
def constraint_columns
@constraint_columns ||= []
end

attr_writer :unauthorized_columns
def unauthorized_columns
@unauthorized_columns ||= []
end

def length
((@set - self.constraint_columns) - self.unauthorized_columns).length
end

protected

Expand Down
8 changes: 6 additions & 2 deletions lib/active_scaffold/data_structures/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,18 @@ def search_joins=(value)
# search = "CONCAT(a, b)" define your own sql for searching. this should be the "left-side" of a WHERE condition. the operator and value will be supplied by ActiveScaffold.
# search = [:a, :b] searches in both fields
def search_sql=(value)
@search_sql = (value == true || value.is_a?(Proc)) ? value : Array(value)
@search_sql = if value
(value == true || value.is_a?(Proc)) ? value : Array(value)
else
value
end
end
def search_sql
self.initialize_search_sql if @search_sql === true
@search_sql
end
def searchable?
search_sql != false && search_sql != nil
!!search_sql
end

# to modify the default order of columns
Expand Down
14 changes: 1 addition & 13 deletions lib/active_scaffold/finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def condition_value_for_datetime(column, value, conversion = :to_time)

def condition_value_for_numeric(column, value)
return value if value.nil?
value = i18n_number_to_native_format(value) if [:i18n_number, :currency].include?(column.options[:format]) && column.search_ui != :number
value = column.number_to_native(value) if column.options[:format] && column.search_ui != :number
case (column.search_ui || column.column.type)
when :integer then value.to_i rescue value ? 1 : 0
when :float then value.to_f
Expand All @@ -184,18 +184,6 @@ def condition_value_for_numeric(column, value)
value
end
end

def i18n_number_to_native_format(value)
native = '.'
delimiter = I18n.t('number.format.delimiter')
separator = I18n.t('number.format.separator')
return value if value.blank? || !value.is_a?(String)
unless delimiter == native && !value.include?(separator) && value !~ /\.\d{3}$/
value.gsub(/[^0-9\-#{I18n.t('number.format.separator')}]/, '').gsub(I18n.t('number.format.separator'), native)
else
value
end
end

def datetime_conversion_for_condition(column)
if column.column
Expand Down
6 changes: 3 additions & 3 deletions test/data_structures/action_columns_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '../test_helper.rb')
require 'test_helper'
# require 'test/model_stub'
require File.join(File.dirname(__FILE__), '../../lib/active_scaffold/data_structures/set.rb')
#require File.join(File.dirname(__FILE__), '../../lib/active_scaffold/data_structures/set.rb')

class ActionColumnsTest < Test::Unit::TestCase
def setup
Expand Down Expand Up @@ -110,4 +110,4 @@ def test_include
assert @columns.include?(:c)
assert !@columns.include?(:d)
end
end
end
2 changes: 1 addition & 1 deletion test/data_structures/action_link_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.join(File.dirname(__FILE__), '../test_helper.rb')
require 'test_helper'

class ActionLinkTest < Test::Unit::TestCase
def setup
Expand Down
6 changes: 3 additions & 3 deletions test/data_structures/action_links_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.join(File.dirname(__FILE__), '../test_helper.rb')
require 'test_helper'

class ActionLinksTest < Test::Unit::TestCase
def setup
Expand Down Expand Up @@ -53,10 +53,10 @@ def test_each
@links.add 'foo', :type => :collection
@links.add 'bar', :type => :member

@links.each :collection do |link|
@links.collection.each do |link|
assert_equal 'foo', link.action
end
@links.each :member do |link|
@links.member.each do |link|
assert_equal 'bar', link.action
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/data_structures/actions_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.join(File.dirname(__FILE__), '../test_helper.rb')
require 'test_helper'

class ActionsTest < Test::Unit::TestCase
def setup
Expand All @@ -22,4 +22,4 @@ def test_add
@actions.add 'c'
assert @actions.include?('c')
end
end
end
11 changes: 5 additions & 6 deletions test/data_structures/association_column_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require File.join(File.dirname(__FILE__), '../test_helper.rb')
require File.join(File.dirname(__FILE__), '../model_stub')
require 'test_helper'
require 'model_stub'

class AssociationColumnTest < Test::Unit::TestCase
def setup
Expand All @@ -12,15 +12,14 @@ def test_virtuality
end

def test_sorting
# sorting on association columns is method-based
hash = {:method => "other_model.to_s"}
assert_equal hash, @association_column.sort
# sorting on association columns is not defined
assert_equal false, @association_column.sort
end

def test_searching
# by default searching on association columns uses primary key
assert @association_column.searchable?
assert_equal '"model_stubs"."id"', @association_column.search_sql
assert_equal ['"model_stubs"."id"'], @association_column.search_sql
end

def test_association
Expand Down
8 changes: 4 additions & 4 deletions test/data_structures/column_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.join(File.dirname(__FILE__), '../test_helper.rb')
require 'test_helper'

class ColumnTest < Test::Unit::TestCase
def setup
Expand Down Expand Up @@ -121,9 +121,9 @@ def test_sortable

def test_custom_search
@column.search_sql = true
assert_equal '"model_stubs"."a"', @column.search_sql
assert_equal ['"model_stubs"."a"'], @column.search_sql
@column.search_sql = 'foobar'
assert_equal 'foobar', @column.search_sql
assert_equal ['foobar'], @column.search_sql
assert @column.searchable?
end

Expand Down Expand Up @@ -172,7 +172,7 @@ def test_action_link
end

def test_includes
assert_equal [], @column.includes
assert_equal nil, @column.includes

# make sure that when a non-array comes in, an array comes out
@column.includes = :column_name
Expand Down
5 changes: 2 additions & 3 deletions test/data_structures/columns_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require File.join(File.dirname(__FILE__), '../test_helper.rb')
# require 'test/model_stub'
require 'test_helper'

class ColumnsTest < Test::Unit::TestCase
def setup
Expand Down Expand Up @@ -66,4 +65,4 @@ def test_block_config
assert @columns.include?(:d)
assert @columns.include?(:c)
end
end
end
4 changes: 2 additions & 2 deletions test/data_structures/error_message_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.join(File.dirname(__FILE__), '../test_helper.rb')
require 'test_helper'

class ErrorMessageTest < Test::Unit::TestCase
def setup
Expand All @@ -25,4 +25,4 @@ def test_yaml
assert yml.has_key?(:error)
assert_equal 'foo', yml[:error]
end
end
end
Loading

0 comments on commit fb38fab

Please sign in to comment.