flexselect 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.0
1
+ 1.3.0
data/lib/flexselect.rb CHANGED
@@ -1,4 +1,9 @@
1
1
  module Flexselect
2
+ DEFAULT = {
3
+ :block => lambda { |new| new },
4
+ :if => lambda { |new| !new.nil? },
5
+ :on => :before_validation
6
+ }
2
7
 
3
8
  # Creates an virtual attribute to handle new values for
4
9
  # an autocomplete select tag handled by flexquery and a callback
@@ -26,14 +31,16 @@ module Flexselect
26
31
  # &block :: If provided, is called to get the new value for the attribute.
27
32
  # Defaults to lambda{ |virtual_attribute_value| virtual_attribute_value }
28
33
  #
34
+ # NOTE default values can be changed by tweaking the :block, :if and :on keys of the
35
+ # Flexselect::DEFAULT hash.
29
36
  def attr_flex_selector(attr, options = {}, &block)
30
37
  virtual_attribute = "flex_#{ attr }_input"
31
38
  callback_method = "callback_for_#{ virtual_attribute }"
32
39
 
33
40
  attr_accessor virtual_attribute
34
41
 
35
- if_lambda = options[:if] || lambda { |new| new.present? }
36
- action_lambda = block || lambda { |new| new }
42
+ if_lambda = options[:if] || DEFAULT[:if]
43
+ action_lambda = block || DEFAULT[:block]
37
44
 
38
45
  # A new Module allows to redefine method retaining the old one for "super" usage.
39
46
  mod = Module.new do
@@ -49,7 +56,7 @@ module Flexselect
49
56
 
50
57
  include mod
51
58
 
52
- send(options[:on] || :before_validation_on_create, callback_method)
59
+ send(options[:on] || DEFAULT[:on], callback_method)
53
60
  end
54
61
 
55
62
  end
@@ -4,6 +4,6 @@ ActiveRecord::Base.logger = Logger.new(STDOUT)
4
4
 
5
5
  ActiveRecord::Schema.define do
6
6
  create_table "things" do |t|
7
- t.string :a, :b, :c, :d
7
+ t.string :a, :b, :c, :d, :e
8
8
  end
9
9
  end
@@ -57,4 +57,21 @@ describe "Flexselect" do
57
57
  thing.d.should == "a-b-c-flex d-flex d"
58
58
  end
59
59
  end
60
+
61
+ describe "changing the defaults" do
62
+ before(:all) do
63
+ temporal_hash_change(Flexselect::DEFAULT, :block => lambda{ "something new" }, :on => :after_create, :if => lambda{|v|v=="sarasa"}) do
64
+ Thing.class_eval do
65
+ attr_flex_selector :e
66
+ end
67
+ end
68
+ end
69
+
70
+ it "should work with a different set of defaults" do
71
+ Thing.create!(:e => "hola").e.should == "hola"
72
+ Thing.create!(:flex_e_input => "hola").e.should == nil
73
+ Thing.create!(:flex_e_input => "sarasa").e.should == "something new"
74
+ Thing.after_create_callback_chain.map(&:method).map(&:to_s).should include("callback_for_flex_e_input")
75
+ end
76
+ end
60
77
  end
data/spec/spec_helper.rb CHANGED
@@ -11,5 +11,17 @@ require 'active_record_helper'
11
11
  # Rails hook.
12
12
  require(File.join(File.dirname(__FILE__), '..', 'rails', 'init.rb'))
13
13
 
14
+ module SpecUtils
15
+ def temporal_hash_change(hash_to_change, hash_to_assign)
16
+ backup = hash_to_change.dup
17
+ hash_to_change.merge!(hash_to_assign)
18
+ yield(hash_to_change)
19
+ ensure
20
+ hash_to_change.clear.merge!(backup)
21
+ end
22
+ end
23
+
14
24
  Spec::Runner.configure do |config|
25
+ config.include SpecUtils
15
26
  end
27
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexselect
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emmanuel Oga
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-14 00:00:00 -03:00
12
+ date: 2010-02-04 00:00:00 -03:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency