anaf_habtm 0.0.86 → 0.0.87
Sign up to get free protection for your applications and to get access to all the features.
- data/anaf_habtm.gemspec +1 -1
- data/lib/anaf_habtm/anaf_active_record.rb +30 -20
- metadata +3 -3
data/anaf_habtm.gemspec
CHANGED
@@ -1,7 +1,16 @@
|
|
1
1
|
|
2
2
|
module AnafHabtm
|
3
3
|
module ActiveRecord
|
4
|
-
|
4
|
+
def autocomplete_format(&block)
|
5
|
+
class_eval do
|
6
|
+
@autocomplete_block = block
|
7
|
+
def self.to_autocomplete(items)
|
8
|
+
items.map{|t| @autocomplete_block.call(t)}.to_json
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# options = {:named=>"name",
|
5
14
|
# :ar_options=>{:autosave=>true}}
|
6
15
|
def anaf_habtm(association, options={}, &block)
|
7
16
|
raise "Must specify :find or a code block." if (block.nil? && !options.has_key?(:find) && !options[:create])
|
@@ -9,7 +18,7 @@ module AnafHabtm
|
|
9
18
|
# Define a proc that will look up the (potentially) existing object
|
10
19
|
finder = proc {|id| association.to_s.singularize.camelize.constantize.where(:id=>id).first
|
11
20
|
}
|
12
|
-
|
21
|
+
|
13
22
|
# Define a proc that will set the association collection
|
14
23
|
set_collection = proc {|me, coll| me.send("#{association.to_s.tableize}=", coll)}
|
15
24
|
has_and_belongs_to_many association.to_sym, options[:ar_options]||={}
|
@@ -37,7 +46,7 @@ module AnafHabtm
|
|
37
46
|
obj = obj ||= klass.new if options[:create]
|
38
47
|
|
39
48
|
unless obj
|
40
|
-
raise "Couldn't find or create #{association} named #{name}."
|
49
|
+
raise "Couldn't find or create #{association} named #{name}."
|
41
50
|
end
|
42
51
|
end
|
43
52
|
obj.attributes = params_copy
|
@@ -51,7 +60,7 @@ module AnafHabtm
|
|
51
60
|
}
|
52
61
|
end
|
53
62
|
end
|
54
|
-
|
63
|
+
|
55
64
|
def association_text(association, opts={})
|
56
65
|
raise "Must give a name for text_association" unless opts.has_key?(:name)
|
57
66
|
class_eval do
|
@@ -78,7 +87,7 @@ module AnafHabtm
|
|
78
87
|
end
|
79
88
|
self.send("#{association.to_s}=", coll)
|
80
89
|
}
|
81
|
-
|
90
|
+
|
82
91
|
define_method "#{association.to_s}_text", lambda{
|
83
92
|
StringReader.new.write_items(self.send(association.to_s)) do |item|
|
84
93
|
name = item.send(opts[:name])
|
@@ -88,7 +97,7 @@ module AnafHabtm
|
|
88
97
|
}
|
89
98
|
end
|
90
99
|
end
|
91
|
-
|
100
|
+
|
92
101
|
def named_association(member, attribute, opts={})
|
93
102
|
member = member.to_s
|
94
103
|
klass = (opts.has_key?(:class_name) ? opts[:class_name] : member.to_s.singularize.camelize).constantize
|
@@ -109,27 +118,27 @@ module AnafHabtm
|
|
109
118
|
}
|
110
119
|
end
|
111
120
|
end
|
112
|
-
class_eval "def #{member}_#{attribute};
|
121
|
+
class_eval "def #{member}_#{attribute};
|
113
122
|
#{member}.#{attribute} if #{member};
|
114
123
|
end;"
|
115
124
|
end
|
116
|
-
|
125
|
+
|
117
126
|
def search_on(*cols)
|
118
127
|
class_eval "def self.search_columns; #{cols.map{|t| t.to_s}.to_ary.inspect}; end;"
|
119
128
|
class_eval do
|
120
|
-
scope :search, lambda{|str|
|
121
|
-
items = like_condition(str.downcase)
|
122
|
-
if scopes.has_key?(:search_mod)
|
123
|
-
items = items.search_mod
|
124
|
-
end
|
125
|
-
items
|
126
|
-
}
|
129
|
+
# scope :search, lambda{|str|
|
130
|
+
# items = like_condition(str.downcase)
|
131
|
+
# if scopes.has_key?(:search_mod)
|
132
|
+
# items = items.search_mod
|
133
|
+
# end
|
134
|
+
# items
|
135
|
+
# }
|
127
136
|
scope :with_name, lambda{|str|
|
128
137
|
equals_condition(str.downcase).limit(1)
|
129
138
|
}
|
130
139
|
end
|
131
140
|
end
|
132
|
-
|
141
|
+
|
133
142
|
def lookup(params)
|
134
143
|
str = params[:id]
|
135
144
|
if str.match(/\D/)
|
@@ -146,14 +155,15 @@ module AnafHabtm
|
|
146
155
|
def equals_condition(str)
|
147
156
|
where(condition("= '#{str.downcase}'"))
|
148
157
|
end
|
149
|
-
|
150
|
-
|
158
|
+
|
159
|
+
|
151
160
|
def named(str)
|
152
161
|
with_name(str).first
|
153
162
|
end
|
154
163
|
|
155
164
|
def condition(cond)
|
156
165
|
search_columns.map{|c| "trim(lower(#{c})) #{cond}"}.join(" or ")
|
157
|
-
end
|
158
|
-
end
|
166
|
+
end
|
167
|
+
end
|
159
168
|
end
|
169
|
+
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: anaf_habtm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 177
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 87
|
10
|
+
version: 0.0.87
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Tyler Gannon
|