sferik-merb-admin 0.3.6 → 0.4.0

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.
Files changed (48) hide show
  1. data/README.markdown +3 -4
  2. data/Rakefile +1 -1
  3. data/app/controllers/main.rb +13 -51
  4. data/app/helpers/main_helper.rb +7 -1
  5. data/app/views/layout/form.html.erb +1 -1
  6. data/app/views/layout/list.html.erb +1 -1
  7. data/app/views/main/_belongs_to.html.erb +2 -2
  8. data/app/views/main/_big_decimal.html.erb +1 -1
  9. data/app/views/main/_boolean.html.erb +1 -1
  10. data/app/views/main/_date.html.erb +1 -1
  11. data/app/views/main/{_date_time.html.erb → _datetime.html.erb} +1 -1
  12. data/app/views/main/_float.html.erb +1 -1
  13. data/app/views/main/_has_many.html.erb +2 -2
  14. data/app/views/main/_has_one.html.erb +3 -3
  15. data/app/views/main/_integer.html.erb +1 -1
  16. data/app/views/main/_string.html.erb +1 -1
  17. data/app/views/main/_text.html.erb +1 -1
  18. data/app/views/main/_time.html.erb +1 -1
  19. data/app/views/main/_timestamp.html.erb +12 -0
  20. data/app/views/main/delete.html.erb +3 -3
  21. data/app/views/main/edit.html.erb +2 -2
  22. data/app/views/main/index.html.erb +4 -4
  23. data/app/views/main/list.html.erb +6 -63
  24. data/app/views/main/new.html.erb +1 -1
  25. data/lib/abstract_model.rb +27 -6
  26. data/lib/activerecord_support.rb +169 -0
  27. data/lib/datamapper_support.rb +55 -23
  28. data/lib/generic_support.rb +1 -1
  29. data/lib/merb-admin.rb +1 -1
  30. data/spec/models/activerecord/division.rb +8 -0
  31. data/spec/models/activerecord/draft.rb +16 -0
  32. data/spec/models/activerecord/league.rb +6 -0
  33. data/spec/models/activerecord/player.rb +10 -0
  34. data/spec/models/activerecord/team.rb +11 -0
  35. data/spec/models/{division.rb → datamapper/division.rb} +0 -0
  36. data/spec/models/{draft.rb → datamapper/draft.rb} +0 -0
  37. data/spec/models/{league.rb → datamapper/league.rb} +0 -0
  38. data/spec/models/{player.rb → datamapper/player.rb} +1 -2
  39. data/spec/models/{team.rb → datamapper/team.rb} +0 -0
  40. data/spec/requests/main_spec.rb +41 -136
  41. data/spec/spec_helper.rb +57 -21
  42. metadata +17 -15
  43. data/lib/metaid.rb +0 -19
  44. data/spec/fixtures/division_fixture.rb +0 -4
  45. data/spec/fixtures/draft_fixture.rb +0 -10
  46. data/spec/fixtures/league_fixture.rb +0 -3
  47. data/spec/fixtures/player_fixture.rb +0 -7
  48. data/spec/fixtures/team_fixture.rb +0 -5
@@ -0,0 +1,169 @@
1
+ module MerbAdmin
2
+ class AbstractModel
3
+ module ActiverecordSupport
4
+ def count(options = {})
5
+ model.count(options)
6
+ end
7
+
8
+ def get(id)
9
+ model.find(id).extend(InstanceMethods)
10
+ rescue
11
+ nil
12
+ end
13
+
14
+ def first(options = {})
15
+ model.first(options).extend(InstanceMethods)
16
+ end
17
+
18
+ def all(options = {})
19
+ model.all(options)
20
+ end
21
+
22
+ def all_in(ids, options = {})
23
+ options[:conditions] = ["id IN (?)", ids]
24
+ model.all(options)
25
+ end
26
+
27
+ def paginated(options = {})
28
+ page = options.delete(:page) || 1
29
+ per_page = options.delete(:per_page) || MerbAdmin[:per_page]
30
+
31
+ page_count = (count(options).to_f / per_page).ceil
32
+
33
+ options.merge!({
34
+ :limit => per_page,
35
+ :offset => (page - 1) * per_page
36
+ })
37
+
38
+ [page_count, all(options)]
39
+ end
40
+
41
+ def create(params = {})
42
+ model.create(params)
43
+ end
44
+
45
+ def new(params = {})
46
+ model.new(params).extend(InstanceMethods)
47
+ end
48
+
49
+ def destroy_all!
50
+ model.all.each do |object|
51
+ object.destroy
52
+ end
53
+ end
54
+
55
+ def has_many_associations
56
+ associations.select do |association|
57
+ association[:type] == :has_many
58
+ end
59
+ end
60
+
61
+ def has_one_associations
62
+ associations.select do |association|
63
+ association[:type] == :has_one
64
+ end
65
+ end
66
+
67
+ def belongs_to_associations
68
+ associations.select do |association|
69
+ association[:type] == :belongs_to
70
+ end
71
+ end
72
+
73
+ def associations
74
+ model.reflect_on_all_associations.map do |association|
75
+ {
76
+ :name => association.name,
77
+ :pretty_name => association.name.to_s.gsub('_', ' ').capitalize,
78
+ :type => association.macro,
79
+ :parent_model => association_parent_model_lookup(association),
80
+ :parent_key => association_parent_key_lookup(association),
81
+ :child_model => association_child_model_lookup(association),
82
+ :child_key => association_child_key_lookup(association),
83
+ }
84
+ end
85
+ end
86
+
87
+ def properties
88
+ model.columns.map do |property|
89
+ {
90
+ :name => property.name.to_sym,
91
+ :pretty_name => property.human_name,
92
+ :type => property.type,
93
+ :length => property.limit,
94
+ :nullable? => property.null,
95
+ :serial? => property.primary,
96
+ :key? => property.primary,
97
+ :flag_map => property.type.respond_to?(:flag_map) ? property.type.flag_map : nil,
98
+ }
99
+ end
100
+ end
101
+
102
+ private
103
+
104
+ def association_parent_model_lookup(association)
105
+ case association.macro
106
+ when :belongs_to
107
+ association.klass
108
+ when :has_one, :has_many
109
+ association.active_record
110
+ else
111
+ raise "Unknown association type"
112
+ end
113
+ end
114
+
115
+ def association_parent_key_lookup(association)
116
+ [:id]
117
+ end
118
+
119
+ def association_child_model_lookup(association)
120
+ case association.macro
121
+ when :belongs_to
122
+ association.active_record
123
+ when :has_one, :has_many
124
+ association.klass
125
+ else
126
+ raise "Unknown association type"
127
+ end
128
+ end
129
+
130
+ def association_child_key_lookup(association)
131
+ case association.macro
132
+ when :belongs_to
133
+ ["#{association.class_name.snake_case}_id".to_sym]
134
+ when :has_one, :has_many
135
+ [association.primary_key_name.to_sym]
136
+ else
137
+ raise "Unknown association type"
138
+ end
139
+ end
140
+
141
+ module InstanceMethods
142
+ def id
143
+ super
144
+ end
145
+
146
+ def save
147
+ super
148
+ end
149
+
150
+ def destroy
151
+ super
152
+ end
153
+
154
+ def update_attributes(attributes)
155
+ super
156
+ end
157
+
158
+ def errors
159
+ super
160
+ end
161
+
162
+ def clear_association(association)
163
+ association.clear
164
+ end
165
+ end
166
+
167
+ end
168
+ end
169
+ end
@@ -5,20 +5,27 @@ module MerbAdmin
5
5
  model.count(options)
6
6
  end
7
7
 
8
- def find_all(options = {})
8
+ def get(id)
9
+ model.get(id).extend(InstanceMethods)
10
+ end
11
+
12
+ def first(options = {})
13
+ model.first(options).extend(InstanceMethods)
14
+ end
15
+
16
+ def all(options = {})
9
17
  model.all(options)
10
18
  end
11
19
 
12
- def find(id)
13
- model.get(id).extend(InstanceMethods)
20
+ def all_in(ids, options = {})
21
+ options[:id] = ids
22
+ model.all(options)
14
23
  end
15
24
 
16
25
  def paginated(options = {})
17
26
  page = options.delete(:page) || 1
18
27
  per_page = options.delete(:per_page) || MerbAdmin[:per_page]
19
28
 
20
- options[:order] ||= [:id.desc]
21
-
22
29
  page_count = (count(options).to_f / per_page).ceil
23
30
 
24
31
  options.merge!({
@@ -26,13 +33,21 @@ module MerbAdmin
26
33
  :offset => (page - 1) * per_page
27
34
  })
28
35
 
29
- [page_count, find_all(options)]
36
+ [page_count, all(options)]
37
+ end
38
+
39
+ def create(params = {})
40
+ model.create(params)
30
41
  end
31
42
 
32
43
  def new(params = {})
33
44
  model.new(params).extend(InstanceMethods)
34
45
  end
35
46
 
47
+ def destroy_all!
48
+ model.all.destroy!
49
+ end
50
+
36
51
  def has_many_associations
37
52
  associations.select do |association|
38
53
  association[:type] == :has_many
@@ -52,17 +67,15 @@ module MerbAdmin
52
67
  end
53
68
 
54
69
  def associations
55
- model.relationships.to_a.map do |name, relationship|
70
+ model.relationships.to_a.map do |name, association|
56
71
  {
57
72
  :name => name,
58
- :pretty_name => name.to_s.gsub('_', ' '),
59
- :type => association_type_lookup(relationship),
60
- :parent_model => relationship.parent_model,
61
- :parent_key => relationship.parent_key.map{|r| r.name},
62
- :child_model => relationship.child_model,
63
- :child_key => relationship.child_key.map{|r| r.name},
64
- :remote_relationship => relationship.options[:remote_relationship_name],
65
- :near_relationship => relationship.options[:near_relationship_name],
73
+ :pretty_name => name.to_s.gsub('_', ' ').capitalize,
74
+ :type => association_type_lookup(association),
75
+ :parent_model => association.parent_model,
76
+ :parent_key => association.parent_key.map{|r| r.name},
77
+ :child_model => association.child_model,
78
+ :child_key => association.child_key.map{|r| r.name},
66
79
  }
67
80
  end
68
81
  end
@@ -71,7 +84,7 @@ module MerbAdmin
71
84
  model.properties.map do |property|
72
85
  {
73
86
  :name => property.name,
74
- :pretty_name => property.field.gsub('_', ' '),
87
+ :pretty_name => property.name.to_s.gsub('_', ' ').capitalize,
75
88
  :type => type_lookup(property),
76
89
  :length => property.length,
77
90
  :nullable? => property.nullable?,
@@ -84,10 +97,10 @@ module MerbAdmin
84
97
 
85
98
  private
86
99
 
87
- def association_type_lookup(relationship)
88
- if self.model == relationship.parent_model
89
- relationship.options[:max] > 1 ? :has_many : :has_one
90
- elsif self.model == relationship.child_model
100
+ def association_type_lookup(association)
101
+ if self.model == association.parent_model
102
+ association.options[:max] > 1 ? :has_many : :has_one
103
+ elsif self.model == association.child_model
91
104
  :belongs_to
92
105
  else
93
106
  raise "Unknown association type"
@@ -99,22 +112,41 @@ module MerbAdmin
99
112
  BigDecimal => :big_decimal,
100
113
  DataMapper::Types::Boolean => :boolean,
101
114
  DataMapper::Types::ParanoidBoolean => :boolean,
102
- DataMapper::Types::ParanoidDateTime => :date_time,
115
+ DataMapper::Types::ParanoidDateTime => :datetime,
103
116
  DataMapper::Types::Serial => :integer,
104
117
  DataMapper::Types::Text => :text,
105
118
  Date => :date,
106
- DateTime => :date_time,
119
+ DateTime => :datetime,
107
120
  Fixnum => :integer,
108
121
  Float => :float,
109
122
  Integer => :integer,
110
123
  String => :string,
111
124
  Time => :time,
112
- TrueClass => :boolean,
113
125
  }
114
126
  type[property.type] || type[property.primitive]
115
127
  end
116
128
 
117
129
  module InstanceMethods
130
+ def id
131
+ super
132
+ end
133
+
134
+ def save
135
+ super
136
+ end
137
+
138
+ def destroy
139
+ super
140
+ end
141
+
142
+ def update_attributes(attributes)
143
+ super
144
+ end
145
+
146
+ def errors
147
+ super
148
+ end
149
+
118
150
  def clear_association(association)
119
151
  association.clear
120
152
  end
@@ -10,7 +10,7 @@ module MerbAdmin
10
10
  end
11
11
 
12
12
  def pretty_name
13
- model.to_s.snake_case.gsub('_', ' ')
13
+ model.to_s.snake_case.gsub('_', ' ').capitalize
14
14
  end
15
15
  end
16
16
  end
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.6"
26
+ self.version = "0.4.0"
27
27
  self.author = "Erik Michaels-Ober"
28
28
 
29
29
  # Stub classes loaded hook - runs before LoadClasses BootLoader
@@ -0,0 +1,8 @@
1
+ class Division < ActiveRecord::Base
2
+ validates_presence_of :league_id
3
+ validates_numericality_of :league_id
4
+ validates_presence_of :name
5
+
6
+ belongs_to :league
7
+ has_many :teams
8
+ end
@@ -0,0 +1,16 @@
1
+ class Draft < ActiveRecord::Base
2
+ validates_presence_of :player_id
3
+ validates_numericality_of :player_id
4
+ validates_presence_of :team_id
5
+ validates_numericality_of :team_id
6
+ validates_presence_of :date
7
+ validates_presence_of :round
8
+ validates_numericality_of :round
9
+ validates_presence_of :pick
10
+ validates_numericality_of :pick
11
+ validates_presence_of :overall
12
+ validates_numericality_of :overall
13
+
14
+ belongs_to :team
15
+ belongs_to :player
16
+ end
@@ -0,0 +1,6 @@
1
+ class League < ActiveRecord::Base
2
+ validates_presence_of :name
3
+
4
+ has_many :divisions
5
+ has_many :teams
6
+ end
@@ -0,0 +1,10 @@
1
+ class Player < ActiveRecord::Base
2
+ validates_presence_of :team_id
3
+ validates_numericality_of :team_id
4
+ validates_presence_of :name
5
+ validates_presence_of :number
6
+ validates_numericality_of :number
7
+
8
+ belongs_to :team
9
+ has_one :draft
10
+ end
@@ -0,0 +1,11 @@
1
+ class Team < ActiveRecord::Base
2
+ validates_presence_of :league_id
3
+ validates_numericality_of :league_id
4
+ validates_presence_of :division_id
5
+ validates_numericality_of :division_id
6
+ validates_presence_of :name
7
+
8
+ belongs_to :league
9
+ belongs_to :division
10
+ has_many :players
11
+ end
File without changes
File without changes
File without changes
@@ -6,13 +6,12 @@ class Player
6
6
  property :updated_at, DateTime
7
7
  property :deleted_at, ParanoidDateTime
8
8
  property :team_id, Integer, :nullable => false, :index => true
9
- property :number, Integer, :nullable => false
10
9
  property :name, String, :length => 100, :nullable => false
10
+ property :number, Integer, :nullable => false
11
11
  property :position, Enum[:pitcher, :catcher, :first, :second, :third, :shortstop, :left, :center, :right]
12
12
  property :sex, Enum[:male, :female]
13
13
  property :batting_average, Float, :default => 0.0, :precision => 4, :scale => 3
14
14
  property :injured, Boolean, :default => false
15
- property :retired, TrueClass, :default => false
16
15
  property :born_on, Date
17
16
  property :wake_at, Time
18
17
  property :notes, Text
File without changes