sequel-crushyform 0.0.8 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -140,6 +140,7 @@ TYPES OF FIELD
140
140
  - :datetime is in the format YYYY-MM-DD HH:MM:SS because it is accepted by sequel setters as-is
141
141
  - :parent is a dropdown list to chose from
142
142
  - :attachment is for attachments (who guessed?).
143
+ - :select could be used for fields like String or Fixnum but giving a limited number of options
143
144
 
144
145
  MORE ABOUT DATE/TIME FIELDS
145
146
  ---------------------------
@@ -228,6 +229,57 @@ Crushyform turns a multi-line text in a one liner if it is the label column.
228
229
 
229
230
  You get the idea.
230
231
 
232
+ MORE ABOUT SELECT FIELDS
233
+ ========================
234
+
235
+ This basically the kind of field you want when you have a text field but people are limited to a dropdown list of options.
236
+ Parent field could be in that category in fact. That is a Fixnum, but limited to available foreign keys.
237
+ Another example would be a list like your Favourite editor in a list:
238
+
239
+ - Emacs
240
+ - Vi
241
+ - Ed
242
+ - Sam
243
+ - Other
244
+
245
+ To achieve that, you can set something like that:
246
+
247
+ class Profile < ::Sequel::Model
248
+ plugin :schema
249
+ set_schema do
250
+ primary_key :id
251
+ String :fave_editor, :crushyform=>{ :type=>:select, :select_options=>['Emacs', 'Vi', 'Ed', 'Sam', 'Other'] }
252
+ end
253
+ end
254
+
255
+ This will create an appropriate dropdown instead of textfield. But the value is also what people see.
256
+ Maybe you want to display the name of an editor, but what you want to record is a score from 0 to 10.
257
+ This is not necessarily an integer as the purpose is just to show you how you make a dropdown with values different from what is displayed:
258
+
259
+ class Profile < ::Sequel::Model
260
+ plugin :schema
261
+ set_schema do
262
+ primary_key :id
263
+ Fixnum :how_much_you_worth, :crushyform=>{ :type=>:select, :select_options=>[['Emacs',5], ['Vi',5], ['Ed',10], ['Sam',9], ['Other', 0]] }
264
+ end
265
+ end
266
+
267
+ You simply put a key/value Array instead of the bare value. Pretty straight forward.
268
+ And you can also provide the name of an instance method instead as you might want an Array with dynamic content:
269
+
270
+ class Profile < ::Sequel::Model
271
+ plugin :schema
272
+ set_schema do
273
+ primary_key :id
274
+ String :hero, :crushyform=>{ :type=>:select, :select_options=> :draw_me_a_list }
275
+ end
276
+ def draw_me_a_list
277
+ # Produce an Array dynamically
278
+ end
279
+ end
280
+
281
+ That's all.
282
+
231
283
  CRUSHYFORM SCHEMA
232
284
  =================
233
285
 
@@ -305,6 +357,7 @@ CHANGE LOG
305
357
  0.0.6 Human name for classes and label for new records
306
358
  0.0.7 Fix `Model#to_label`
307
359
  0.0.8 Fix bug with apostrophe in text fields
360
+ 0.1.0 Add `:select` type and make `:parent` type overridable through `set_schema`
308
361
 
309
362
  COPYRIGHT
310
363
  =========
@@ -1,7 +1,7 @@
1
1
  module ::Sequel::Plugins::Crushyform
2
2
 
3
3
  module ClassMethods
4
- def crushyform_version; [0,0,8]; end
4
+ def crushyform_version; [0,1,0]; end
5
5
  # Schema
6
6
  def crushyform_schema
7
7
  @crushyform_schema ||= default_crushyform_schema
@@ -15,8 +15,8 @@ module ::Sequel::Plugins::Crushyform
15
15
  {:type=>v[:type]}
16
16
  end
17
17
  end
18
- @schema.columns.each{|c|out[c[:name]]=out[c[:name]].update(c[:crushyform]) if c.has_key?(:crushyform)} if respond_to?(:schema)
19
18
  association_reflections.each{|k,v|out[v[:key]]={:type=>:parent} if v[:type]==:many_to_one}
19
+ @schema.columns.each{|c|out[c[:name]]=out[c[:name]].update(c[:crushyform]) if c.has_key?(:crushyform)} if respond_to?(:schema)
20
20
  out
21
21
  end
22
22
  # Types
@@ -61,6 +61,18 @@ module ::Sequel::Plugins::Crushyform
61
61
  end,
62
62
  :attachment => proc do |m,c,o|
63
63
  "%s<input type='file' name='%s' id='%s' class='%s' />%s\n" % [m.to_thumb(c), o[:input_name], m.crushyid_for(c), o[:input_class], o[:required]]
64
+ end,
65
+ :select => proc do |m,c,o|
66
+ out = "<select name='%s' id='%s' class='%s'>\n" % [o[:input_name], m.crushyid_for(c), o[:input_class]]
67
+ o[:select_options] = m.__send__(o[:select_options]) unless o[:select_options].kind_of?(Array)
68
+ if o[:select_options].kind_of?(Array)
69
+ o[:select_options].each do |op|
70
+ key,val = op.kind_of?(Array) ? [op[0],op[1]] : [op,op]
71
+ selected = 'selected' if val==o[:input_value]
72
+ out << "<option value='%s' %s>%s</option>\n" % [val,selected,key]
73
+ end
74
+ end
75
+ out << "</select>%s\n" % [o[:required]]
64
76
  end
65
77
  }
66
78
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'sequel-crushyform'
3
- s.version = "0.0.8"
3
+ s.version = "0.1.0"
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.summary = "A Sequel plugin that helps building forms"
6
6
  s.description = "A Sequel plugin that helps building forms. It basically does them for you so that you can forget about the boring part. The kind of thing which is good to have in your toolbox for building a CMS."
@@ -1,6 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'bacon'
3
- Bacon.summary_on_exit
3
+ #Bacon.summary_on_exit
4
4
 
5
5
  F = ::File
6
6
  D = ::Dir
@@ -19,9 +19,11 @@ class Haiku < ::Sequel::Model
19
19
  text :body
20
20
  Boolean :published
21
21
  foreign_key :author_id, :authors
22
+ foreign_key :season_id, :crushyform=>{:type=>:string}
22
23
  end
23
24
  create_table unless table_exists?
24
25
  many_to_one :author
26
+ many_to_one :season
25
27
  one_to_many :reviews
26
28
  def validate
27
29
  errors[:title] << "is not good"
@@ -53,6 +55,16 @@ class Review < ::Sequel::Model
53
55
  many_to_one :haiku
54
56
  end
55
57
 
58
+ class Season < ::Sequel::Model
59
+ plugin :schema
60
+ set_schema do
61
+ primary_key :id
62
+ String :name
63
+ end
64
+ create_table unless table_exists?
65
+ one_to_many :haikus
66
+ end
67
+
56
68
  class TestDateTime < ::Sequel::Model
57
69
  plugin :schema
58
70
  set_schema do
@@ -78,6 +90,21 @@ class ShippingAddress < ::Sequel::Model
78
90
  end
79
91
  ShippingAddress.create(:address_body=>"3 Mulholland Drive\n\rFlat C", :postcode=>'90210', :city=>'Richville')
80
92
 
93
+ class Profile < ::Sequel::Model
94
+ plugin :schema
95
+ set_schema do
96
+ primary_key :id
97
+ String :fave_lang, :crushyform=>{:type=>:select,:select_options=>['ruby', 'forth', 'asm']}
98
+ String :fave_os, :crushyform=>{:type=>:select,:select_options=>[['GNU/Linux','sucker'], ['FreeBSD','saner'], ['Mac OSX','wanker'], ['Windows','loser']]}
99
+ String :fave_editor, :crushyform=>{:type=>:select,:select_options=>:editor_list}
100
+ String :fave_error, :crushyform=>{:type=>:select,:select_options=>:error_list}
101
+ Fixnum :fave_number, :crushyform=>{:type=>:select,:select_options=>[0,1,2,3,4,5,6,7,8,9]}
102
+ end
103
+ create_table unless table_exists?
104
+ def editor_list; ['emacs','vi','ed','sam']; end
105
+ end
106
+ Profile.create(:fave_lang=>'forth', :fave_os=>'saner', :fave_editor=>'ed', :fave_number=>3)
107
+
81
108
  require 'stash_magic'
82
109
  class Attached < ::Sequel::Model
83
110
  plugin :schema
@@ -141,7 +168,8 @@ describe 'Crushyform when schema plugin is used' do
141
168
  :title => {:type=>:custom,:name=>'Nice Title'},
142
169
  :body => {:type=>:text},
143
170
  :published => {:type=>:boolean},
144
- :author_id => {:type=>:parent}
171
+ :author_id => {:type=>:parent},
172
+ :season_id => {:type=>:string}
145
173
  }
146
174
  end
147
175
 
@@ -337,7 +365,7 @@ describe 'Crushyfield types' do
337
365
  should 'build parent field with a wrapped version of parent_model#to_dropdown' do
338
366
  Haiku.new.crushyinput(:author_id).should.match(/^<select.*>#{Regexp.escape Author.to_dropdown}<\/select>$/)
339
367
  end
340
-
368
+
341
369
  should 'display a preview with an attachment field whenever it is possible' do
342
370
  a = Attached.new.set(:filename=>'/book.png')
343
371
  a.crushyinput(:filename).should.match(/^#{Regexp.escape a.to_thumb(:filename)}<input type='file'.*\/>\n$/)
@@ -362,6 +390,41 @@ describe 'Crushyfield types' do
362
390
  Haiku.new.crushyfield(:title).should.match(/^<p class='crushyfield '/)
363
391
  end
364
392
 
393
+ describe 'Select' do
394
+ should 'Create a select dropdown out of an array' do
395
+ s = Profile[1].crushyfield(:fave_lang)
396
+ s.should.match(/<select name='model\[fave_lang\]' id='1-Profile-fave_lang' class=''>/)
397
+ s.scan(/<option/).size.should==3
398
+ s.scan(/selected/).size.should==1
399
+ s.should.match(/<option value='forth' selected>forth<\/option>/)
400
+ s.should.match(/ruby.*forth.*asm/m)
401
+ end
402
+ should 'Accept an array of key/value pairs' do
403
+ s = Profile[1].crushyfield(:fave_os)
404
+ s.should.match(/<select name='model\[fave_os\]' id='1-Profile-fave_os' class=''>/)
405
+ s.scan(/<option/).size.should==4
406
+ s.scan(/selected/).size.should==1
407
+ s.should.match(/<option value='saner' selected>FreeBSD<\/option>/)
408
+ s.should.match(/sucker.*saner.*wanker.*loser/m)
409
+ end
410
+ should 'Accept the name of an instance method that generates the Array' do
411
+ s = Profile[1].crushyfield(:fave_editor)
412
+ s.should.match(/<select name='model\[fave_editor\]' id='1-Profile-fave_editor' class=''>/)
413
+ s.scan(/<option/).size.should==4
414
+ s.scan(/selected/).size.should==1
415
+ s.should.match(/<option value='ed' selected>ed<\/option>/)
416
+ end
417
+ should 'Raise if the method is not an instance method' do
418
+ lambda{ Profile[1].crushyfield(:fave_error) }.should.raise(NoMethodError)
419
+ end
420
+ should 'Accept Fixnum columns and place selected correctly' do
421
+ s = Profile[1].crushyfield(:fave_number)
422
+ s.scan(/<option/).size.should==10
423
+ s.scan(/selected/).size.should==1
424
+ s.should.match(/<option value='3' selected>3<\/option>/)
425
+ end
426
+ end
427
+
365
428
  end
366
429
 
367
430
  describe 'Crushyform' do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-crushyform
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 8
10
- version: 0.0.8
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mickael Riga
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-11 00:00:00 +01:00
18
+ date: 2011-08-22 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21