sferik-merb-admin 0.3.2 → 0.3.3

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.
data/README.markdown CHANGED
@@ -14,7 +14,7 @@ At the command prompt, type:
14
14
 
15
15
  In your app, add the following dependency to `config/dependencies.rb`:
16
16
 
17
- dependency "sferik-merb-admin", "0.3.2", :require_as => "merb-admin"
17
+ dependency "sferik-merb-admin", "0.3.3", :require_as => "merb-admin"
18
18
  dependency "dm-is-paginated", "0.0.1" # if you want pagination support
19
19
 
20
20
  Add the following route to `config/router.rb`:
data/Rakefile CHANGED
@@ -12,7 +12,7 @@ AUTHOR = "Erik Michaels-Ober"
12
12
  EMAIL = "sferik@gmail.com"
13
13
  HOMEPAGE = "http://twitter.com/sferik"
14
14
  SUMMARY = "MerbAdmin is a Merb plugin that provides an easy-to-use interface for managing your data."
15
- GEM_VERSION = "0.3.2"
15
+ GEM_VERSION = "0.3.3"
16
16
 
17
17
  spec = Gem::Specification.new do |s|
18
18
  s.rubyforge_project = "merb"
@@ -9,7 +9,7 @@ class MerbAdmin::Main < MerbAdmin::Application
9
9
  before :find_object, :only => ['edit', 'update', 'delete', 'destroy']
10
10
 
11
11
  def index
12
- render(:layout => "dashboard")
12
+ render(:layout => 'dashboard')
13
13
  end
14
14
 
15
15
  def list
@@ -25,7 +25,7 @@ class MerbAdmin::Main < MerbAdmin::Application
25
25
  @objects = @abstract_model.find_all(options).reverse
26
26
  else
27
27
  # monkey patch pagination
28
- @abstract_model.model.class_eval("is_paginated") unless @abstract_model.model.respond_to?(:paginated)
28
+ @abstract_model.model.class_eval('is_paginated') unless @abstract_model.model.respond_to?(:paginated)
29
29
  @current_page = (params[:page] || 1).to_i
30
30
  options = {
31
31
  :page => @current_page,
@@ -39,16 +39,16 @@ class MerbAdmin::Main < MerbAdmin::Application
39
39
  end
40
40
 
41
41
  @record_count = @abstract_model.count(options)
42
- render(:layout => "list")
42
+ render(:layout => 'list')
43
43
  end
44
44
 
45
45
  def new
46
46
  @object = @abstract_model.new
47
- render(:layout => "form")
47
+ render(:layout => 'form')
48
48
  end
49
49
 
50
50
  def edit
51
- render(:layout => "form")
51
+ render(:layout => 'form')
52
52
  end
53
53
 
54
54
  def create
@@ -57,9 +57,10 @@ class MerbAdmin::Main < MerbAdmin::Application
57
57
  object.each do |key, value|
58
58
  object[key] = nil if value.blank?
59
59
  end
60
- associations = @abstract_model.has_many_associations.map{|association| [association, (params[:associations] || {}).delete(association[:name])]}
60
+ has_one_associations = @abstract_model.has_one_associations.map{|association| [association, (params[:associations] || {}).delete(association[:name])]}
61
+ has_many_associations = @abstract_model.has_many_associations.map{|association| [association, (params[:associations] || {}).delete(association[:name])]}
61
62
  @object = @abstract_model.new(object)
62
- if @object.save && associations.each{|association, ids| update_has_many_association(association, ids)}
63
+ if @object.save && has_one_associations.each{|association, id| update_has_one_association(association, id)} && has_many_associations.each{|association, ids| update_has_many_association(association, ids)}
63
64
  if params[:_continue]
64
65
  redirect(slice_url(:admin_edit, :model_name => @abstract_model.singular_name, :id => @object.id), :message => {:notice => "#{@abstract_model.pretty_name.capitalize} was successfully created"})
65
66
  elsif params[:_add_another]
@@ -69,7 +70,7 @@ class MerbAdmin::Main < MerbAdmin::Application
69
70
  end
70
71
  else
71
72
  message[:error] = "#{@abstract_model.pretty_name.capitalize} failed to be created"
72
- render(:new, :layout => "form")
73
+ render(:new, :layout => 'form')
73
74
  end
74
75
  end
75
76
 
@@ -79,8 +80,9 @@ class MerbAdmin::Main < MerbAdmin::Application
79
80
  object.each do |key, value|
80
81
  object[key] = nil if value.blank?
81
82
  end
82
- associations = @abstract_model.has_many_associations.map{|association| [association, (params[:associations] || {}).delete(association[:name])]}
83
- if @object.update_attributes(object) && associations.each{|association, ids| update_has_many_association(association, ids)}
83
+ has_one_associations = @abstract_model.has_one_associations.map{|association| [association, (params[:associations] || {}).delete(association[:name])]}
84
+ has_many_associations = @abstract_model.has_many_associations.map{|association| [association, (params[:associations] || {}).delete(association[:name])]}
85
+ if @object.update_attributes(object) && has_one_associations.each{|association, id| update_has_one_association(association, id)} && has_many_associations.each{|association, ids| update_has_many_association(association, ids)}
84
86
  if params[:_continue]
85
87
  redirect(slice_url(:admin_edit, :model_name => @abstract_model.singular_name, :id => @object.id), :message => {:notice => "#{@abstract_model.pretty_name.capitalize} was successfully updated"})
86
88
  elsif params[:_add_another]
@@ -90,12 +92,12 @@ class MerbAdmin::Main < MerbAdmin::Application
90
92
  end
91
93
  else
92
94
  message[:error] = "#{@abstract_model.pretty_name.capitalize} failed to be updated"
93
- render(:edit, :layout => "form")
95
+ render(:edit, :layout => 'form')
94
96
  end
95
97
  end
96
98
 
97
99
  def delete
98
- render(:layout => "form")
100
+ render(:layout => 'form')
99
101
  end
100
102
 
101
103
  def destroy
@@ -133,7 +135,7 @@ class MerbAdmin::Main < MerbAdmin::Application
133
135
  @properties.each do |property|
134
136
  next unless property[:name] == key.to_sym
135
137
  if property[:type] == :boolean
136
- options.merge!(key.to_sym => (value == "true"))
138
+ options.merge!(key.to_sym => (value == 'true'))
137
139
  elsif property[:type] == :integer && property[:flag_map]
138
140
  options.merge!(key.to_sym => value.to_sym)
139
141
  end
@@ -150,8 +152,8 @@ class MerbAdmin::Main < MerbAdmin::Application
150
152
  condition_statement << "#{property[:name]} LIKE ?"
151
153
  conditions << "%#{params[:query]}%"
152
154
  end
153
- conditions.unshift(condition_statement.join(" OR "))
154
- options.merge!(:conditions => conditions) unless conditions == [""]
155
+ conditions.unshift(condition_statement.join(' OR '))
156
+ options.merge!(:conditions => conditions) unless conditions == ['']
155
157
  end
156
158
 
157
159
  def merge_sort(options)
@@ -159,6 +161,13 @@ class MerbAdmin::Main < MerbAdmin::Application
159
161
  options.merge!(:order => [params[:sort].to_sym.send(params[:sort_reverse] ? :desc : :asc)])
160
162
  end
161
163
 
164
+ def update_has_one_association(association, id)
165
+ model = MerbAdmin::AbstractModel.new(association[:child_model])
166
+ if object = model.find(id)
167
+ object.update_attributes(association[:child_key] => @object.id)
168
+ end
169
+ end
170
+
162
171
  def update_has_many_association(association, ids)
163
172
  # Remove all of the associated items
164
173
  relationship = @object.send(association[:name])
data/lib/merb-admin.rb CHANGED
@@ -23,7 +23,7 @@ if defined?(Merb::Plugins)
23
23
 
24
24
  # Slice metadata
25
25
  self.description = "MerbAdmin is a Merb plugin that provides an easy-to-use interface for managing your data."
26
- self.version = "0.3.2"
26
+ self.version = "0.3.3"
27
27
  self.author = "Erik Michaels-Ober"
28
28
 
29
29
  # Stub classes loaded hook - runs before LoadClasses BootLoader
@@ -20,10 +20,6 @@ describe MerbAdmin::Main do
20
20
  @controller.app_path_for(:image).should == "#{Merb.root}/public/slices/merb-admin/images"
21
21
  @controller.app_path_for(:javascript).should == "#{Merb.root}/public/slices/merb-admin/javascripts"
22
22
  @controller.app_path_for(:stylesheet).should == "#{Merb.root}/public/slices/merb-admin/stylesheets"
23
-
24
- @controller.slice_path_for(:image).should == "#{Merb.root}/public/images"
25
- @controller.slice_path_for(:javascript).should == "#{Merb.root}/public/javascripts"
26
- @controller.slice_path_for(:stylesheet).should == "#{Merb.root}/public/stylesheets"
27
23
  end
28
24
 
29
25
  end
@@ -4,6 +4,13 @@ given "a player exists" do
4
4
  @player = Player.gen
5
5
  end
6
6
 
7
+ given "two drafts exist" do
8
+ @drafts = []
9
+ 2.times do
10
+ @drafts << Draft.gen
11
+ end
12
+ end
13
+
7
14
  given "two players exist" do
8
15
  @players = []
9
16
  2.times do
@@ -11,13 +18,21 @@ given "two players exist" do
11
18
  end
12
19
  end
13
20
 
14
- given "three teams exists" do
21
+ given "three teams exist" do
15
22
  @teams = []
16
23
  3.times do
17
24
  @teams << Team.gen
18
25
  end
19
26
  end
20
27
 
28
+ given "a player exists and two drafts exist" do
29
+ @player = Player.gen
30
+ @drafts = []
31
+ 2.times do
32
+ @drafts << Draft.gen
33
+ end
34
+ end
35
+
21
36
  given "a player exists and three teams exist" do
22
37
  @player = Player.gen
23
38
  @teams = []
@@ -265,7 +280,7 @@ describe "MerbAdmin" do
265
280
  end
266
281
  end
267
282
 
268
- describe "new with has-many association", :given => "three teams exists" do
283
+ describe "new with has-one association", :given => "two drafts exist" do
269
284
  before(:each) do
270
285
  @response = request(url(:admin_new, :model_name => "player"))
271
286
  end
@@ -275,6 +290,15 @@ describe "MerbAdmin" do
275
290
  end
276
291
  end
277
292
 
293
+ describe "new with has-many association", :given => "three teams exist" do
294
+ before(:each) do
295
+ @response = request(url(:admin_new, :model_name => "player"))
296
+ end
297
+
298
+ it "should respond sucessfully" do
299
+ @response.should be_successful
300
+ end
301
+ end
278
302
 
279
303
  describe "edit", :given => "a player exists" do
280
304
  before(:each) do
@@ -290,6 +314,16 @@ describe "MerbAdmin" do
290
314
  end
291
315
  end
292
316
 
317
+ describe "edit with has-one association", :given => "a player exists and two drafts exist" do
318
+ before(:each) do
319
+ @response = request(url(:admin_edit, :model_name => "player", :id => @player.id))
320
+ end
321
+
322
+ it "should respond sucessfully" do
323
+ @response.should be_successful
324
+ end
325
+ end
326
+
293
327
  describe "edit with has-many association", :given => "a player exists and three teams exist" do
294
328
  before(:each) do
295
329
  @response = request(url(:admin_edit, :model_name => "player", :id => @player.id))
@@ -353,7 +387,25 @@ describe "MerbAdmin" do
353
387
  end
354
388
  end
355
389
 
356
- describe "create with has-many association", :given => "three teams exists" do
390
+ describe "create with has-one association", :given => "two drafts exist" do
391
+ before(:each) do
392
+ @response = request(url(:admin_create, :model_name => "player"), :method => "post", :params => {:player => {:name => "Jackie Robinson", :number => 42, :team_id => 1, :position => :second, :sex => :male}, :associations => {:draft => @drafts[0].id}})
393
+ end
394
+
395
+ it "should create a new object" do
396
+ Player.first.should_not be_nil
397
+ end
398
+
399
+ it "should include correct associations" do
400
+ Player.first.draft.should == @drafts[0]
401
+ end
402
+
403
+ it "should not include incorrect associations" do
404
+ Player.first.draft.should_not == @drafts[1]
405
+ end
406
+ end
407
+
408
+ describe "create with has-many association", :given => "three teams exist" do
357
409
  before(:each) do
358
410
  @response = request(url(:admin_create, :model_name => "league"), :method => "post", :params => {:league => {:name => "National League"}, :associations => {:teams => [@teams[0].id, @teams[1].id]}})
359
411
  end
@@ -424,6 +476,24 @@ describe "MerbAdmin" do
424
476
  end
425
477
  end
426
478
 
479
+ describe "update with has-one association", :given => "a player exists and two drafts exist" do
480
+ before(:each) do
481
+ @response = request(url(:admin_update, :model_name => "player", :id => @player.id), :method => "put", :params => {:player => {:name => "Jackie Robinson", :number => 42, :team_id => 1, :position => :second, :sex => :male}, :associations => {:draft => @drafts[0].id}})
482
+ end
483
+
484
+ it "should update an object that already exists" do
485
+ Player.first(:id => @player.id).name.should eql("Jackie Robinson")
486
+ end
487
+
488
+ it "should include correct associations" do
489
+ Player.first.draft.should == @drafts[0]
490
+ end
491
+
492
+ it "should not include incorrect associations" do
493
+ Player.first.draft.should_not == @drafts[1]
494
+ end
495
+ end
496
+
427
497
  describe "update with has-many association", :given => "a league exists and three teams exist" do
428
498
  before(:each) do
429
499
  @response = request(url(:admin_update, :model_name => "league", :id => @league.id), :method => "put", :params => {:league => {:name => "National League"}, :associations => {:teams => [@teams[0].id, @teams[1].id]}})
data/spec/spec_helper.rb CHANGED
@@ -4,7 +4,17 @@ require 'merb-slices'
4
4
  require 'merb-helpers'
5
5
  require 'merb-assets'
6
6
  require 'spec'
7
- require 'spec/fixtures'
7
+
8
+ # Required for fixtures
9
+ require 'dm-sweatshop'
10
+ require 'dm-types'
11
+ require 'dm-aggregates'
12
+ require 'dm-validations'
13
+ require 'dm-is-paginated'
14
+
15
+ Dir[File.join(File.dirname(__FILE__), 'fixtures', '**', '*_fixture.rb').to_s].each do |fixture_file|
16
+ require fixture_file
17
+ end
8
18
 
9
19
  # Add merb-admin.rb to the search path
10
20
  Merb::Plugins.config[:merb_slices][:auto_register] = true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sferik-merb-admin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik Michaels-Ober
@@ -72,7 +72,6 @@ files:
72
72
  - spec/fixtures/league_fixture.rb
73
73
  - spec/fixtures/player_fixture.rb
74
74
  - spec/fixtures/team_fixture.rb
75
- - spec/fixtures.rb
76
75
  - spec/requests
77
76
  - spec/requests/main_spec.rb
78
77
  - spec/spec_helper.rb
@@ -184,7 +183,6 @@ files:
184
183
  - public/stylesheets/widgets.css
185
184
  has_rdoc: false
186
185
  homepage: http://twitter.com/sferik
187
- licenses:
188
186
  post_install_message: |
189
187
  ********************************************************************************
190
188
 
@@ -212,7 +210,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
212
210
  requirements: []
213
211
 
214
212
  rubyforge_project: merb
215
- rubygems_version: 1.3.5
213
+ rubygems_version: 1.2.0
216
214
  signing_key:
217
215
  specification_version: 3
218
216
  summary: MerbAdmin is a Merb plugin that provides an easy-to-use interface for managing your data.
data/spec/fixtures.rb DELETED
@@ -1,11 +0,0 @@
1
- # Learn more at http://github.com/datamapper/dm-more/tree/master/dm-sweatshop
2
- require 'dm-sweatshop'
3
- # Learn more at http://github.com/datamapper/dm-more/tree/master/dm-types
4
- require 'dm-types'
5
- require 'dm-aggregates'
6
- require 'dm-validations'
7
- require 'dm-is-paginated'
8
-
9
- Dir['spec/fixtures/**/*_fixture.rb'].sort.each do |fixture_file|
10
- require fixture_file
11
- end