sferik-merb-admin 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
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.0", :require_as => "merb-admin"
17
+ dependency "sferik-merb-admin", "0.3.1", :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.0"
15
+ GEM_VERSION = "0.3.1"
16
16
 
17
17
  spec = Gem::Specification.new do |s|
18
18
  s.rubyforge_project = "merb"
@@ -59,8 +59,8 @@
59
59
  <thead>
60
60
  <tr>
61
61
  <% @properties.each do |property| %>
62
- <th class="<%= params[:sort] == property[:field] ? params[:sort_reverse] ? 'sorted descending' : 'sorted ascending' : nil %>">
63
- <a href="?<%= Merb::Parse.params_to_query_string(params.merge(:sort => property[:field]).reject{|key, value| key.to_sym == :sort_reverse}.merge(params[:sort] == property[:field] && params[:sort_reverse] != "true" ? {:sort_reverse => "true"} : {})) %>"><%= property[:pretty_name].capitalize %></a>
62
+ <th class="<%= params[:sort] == property[:name].to_s ? params[:sort_reverse] ? 'sorted descending' : 'sorted ascending' : nil %>">
63
+ <a href="?<%= Merb::Parse.params_to_query_string(params.merge(:sort => property[:name]).reject{|key, value| key.to_sym == :sort_reverse}.merge(params[:sort] == property[:name].to_s && params[:sort_reverse] != "true" ? {:sort_reverse => "true"} : {})) %>"><%= property[:pretty_name].capitalize %></a>
64
64
  </th>
65
65
  <% end %>
66
66
  </tr>
@@ -1,6 +1,8 @@
1
+ require 'generic_support'
2
+ require 'datamapper_support'
3
+
1
4
  module MerbAdmin
2
5
  class AbstractModel
3
-
4
6
  # Returns all models for a given Merb app
5
7
  def self.all
6
8
  return @models if @models
@@ -40,131 +42,5 @@ module MerbAdmin
40
42
  raise "MerbAdmin does not currently support the #{Merb.orm} ORM"
41
43
  end
42
44
  end
43
-
44
- module GenericSupport
45
- def singular_name
46
- model.to_s.snake_case.to_sym
47
- end
48
-
49
- def plural_name
50
- model.to_s.snake_case.pluralize.to_sym
51
- end
52
-
53
- def pretty_name
54
- model.to_s.snake_case.gsub('_', ' ')
55
- end
56
- end
57
-
58
- module DatamapperSupport
59
- def count(options = {})
60
- model.count(options)
61
- end
62
-
63
- def find_all(options = {})
64
- model.all(options)
65
- end
66
-
67
- def find(id)
68
- model.get(id).extend(InstanceMethods)
69
- end
70
-
71
- def new(params = {})
72
- model.new(params).extend(InstanceMethods)
73
- end
74
-
75
- def has_many_associations
76
- associations.select do |association|
77
- association[:type] == :has_many
78
- end
79
- end
80
-
81
- def has_one_associations
82
- associations.select do |association|
83
- association[:type] == :has_one
84
- end
85
- end
86
-
87
- def belongs_to_associations
88
- associations.select do |association|
89
- association[:type] == :belongs_to
90
- end
91
- end
92
-
93
- def associations
94
- model.relationships.to_a.map do |name, relationship|
95
- {
96
- :name => name,
97
- :pretty_name => name.to_s.gsub('_', ' '),
98
- :type => association_type_lookup(relationship),
99
- :parent_model => relationship.parent_model,
100
- :parent_key => relationship.parent_key.map{|r| r.name},
101
- :child_model => relationship.child_model,
102
- :child_key => relationship.child_key.map{|r| r.name},
103
- :remote_relationship => relationship.options[:remote_relationship_name],
104
- :near_relationship => relationship.options[:near_relationship_name],
105
- }
106
- end
107
- end
108
-
109
- def association_names
110
- associations.map do |association|
111
- association[:type] == :belongs_to ? association[:parent_name] : association[:child_name]
112
- end
113
- end
114
-
115
- def properties
116
- model.properties.map do |property|
117
- {
118
- :name => property.name,
119
- :pretty_name => property.field.gsub('_', ' '),
120
- :type => type_lookup(property),
121
- :length => property.length,
122
- :nullable? => property.nullable?,
123
- :serial? => property.serial?,
124
- :key? => property.key?,
125
- :flag_map => property.type.respond_to?(:flag_map) ? property.type.flag_map : nil,
126
- }
127
- end
128
- end
129
-
130
- private
131
-
132
- def association_type_lookup(relationship)
133
- if self.model == relationship.parent_model
134
- relationship.options[:max] > 1 ? :has_many : :has_one
135
- elsif self.model == relationship.child_model
136
- :belongs_to
137
- else
138
- raise "Unknown association type"
139
- end
140
- end
141
-
142
- def type_lookup(property)
143
- type = {
144
- DataMapper::Types::Serial => :integer,
145
- DataMapper::Types::Boolean => :boolean,
146
- DataMapper::Types::Text => :text,
147
- DataMapper::Types::ParanoidDateTime => :date_time,
148
- Integer => :integer,
149
- Fixnum => :integer,
150
- Float => :float,
151
- BigDecimal => :big_decimal,
152
- TrueClass => :boolean,
153
- String => :string,
154
- DateTime => :date_time,
155
- Date => :date,
156
- Time => :time,
157
- }
158
- type[property.type] || type[property.primitive]
159
- end
160
-
161
- module InstanceMethods
162
- def clear_association(association)
163
- association.clear
164
- end
165
- end
166
-
167
- end
168
-
169
45
  end
170
46
  end
@@ -0,0 +1,114 @@
1
+ module MerbAdmin
2
+ class AbstractModel
3
+ module DatamapperSupport
4
+ def count(options = {})
5
+ model.count(options)
6
+ end
7
+
8
+ def find_all(options = {})
9
+ model.all(options)
10
+ end
11
+
12
+ def find(id)
13
+ model.get(id).extend(InstanceMethods)
14
+ end
15
+
16
+ def new(params = {})
17
+ model.new(params).extend(InstanceMethods)
18
+ end
19
+
20
+ def has_many_associations
21
+ associations.select do |association|
22
+ association[:type] == :has_many
23
+ end
24
+ end
25
+
26
+ def has_one_associations
27
+ associations.select do |association|
28
+ association[:type] == :has_one
29
+ end
30
+ end
31
+
32
+ def belongs_to_associations
33
+ associations.select do |association|
34
+ association[:type] == :belongs_to
35
+ end
36
+ end
37
+
38
+ def associations
39
+ model.relationships.to_a.map do |name, relationship|
40
+ {
41
+ :name => name,
42
+ :pretty_name => name.to_s.gsub('_', ' '),
43
+ :type => association_type_lookup(relationship),
44
+ :parent_model => relationship.parent_model,
45
+ :parent_key => relationship.parent_key.map{|r| r.name},
46
+ :child_model => relationship.child_model,
47
+ :child_key => relationship.child_key.map{|r| r.name},
48
+ :remote_relationship => relationship.options[:remote_relationship_name],
49
+ :near_relationship => relationship.options[:near_relationship_name],
50
+ }
51
+ end
52
+ end
53
+
54
+ def association_names
55
+ associations.map do |association|
56
+ association[:type] == :belongs_to ? association[:parent_name] : association[:child_name]
57
+ end
58
+ end
59
+
60
+ def properties
61
+ model.properties.map do |property|
62
+ {
63
+ :name => property.name,
64
+ :pretty_name => property.field.gsub('_', ' '),
65
+ :type => type_lookup(property),
66
+ :length => property.length,
67
+ :nullable? => property.nullable?,
68
+ :serial? => property.serial?,
69
+ :key? => property.key?,
70
+ :flag_map => property.type.respond_to?(:flag_map) ? property.type.flag_map : nil,
71
+ }
72
+ end
73
+ end
74
+
75
+ private
76
+
77
+ def association_type_lookup(relationship)
78
+ if self.model == relationship.parent_model
79
+ relationship.options[:max] > 1 ? :has_many : :has_one
80
+ elsif self.model == relationship.child_model
81
+ :belongs_to
82
+ else
83
+ raise "Unknown association type"
84
+ end
85
+ end
86
+
87
+ def type_lookup(property)
88
+ type = {
89
+ DataMapper::Types::Serial => :integer,
90
+ DataMapper::Types::Boolean => :boolean,
91
+ DataMapper::Types::Text => :text,
92
+ DataMapper::Types::ParanoidDateTime => :date_time,
93
+ Integer => :integer,
94
+ Fixnum => :integer,
95
+ Float => :float,
96
+ BigDecimal => :big_decimal,
97
+ TrueClass => :boolean,
98
+ String => :string,
99
+ DateTime => :date_time,
100
+ Date => :date,
101
+ Time => :time,
102
+ }
103
+ type[property.type] || type[property.primitive]
104
+ end
105
+
106
+ module InstanceMethods
107
+ def clear_association(association)
108
+ association.clear
109
+ end
110
+ end
111
+
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,17 @@
1
+ module MerbAdmin
2
+ class AbstractModel
3
+ module GenericSupport
4
+ def singular_name
5
+ model.to_s.snake_case.to_sym
6
+ end
7
+
8
+ def plural_name
9
+ model.to_s.snake_case.pluralize.to_sym
10
+ end
11
+
12
+ def pretty_name
13
+ model.to_s.snake_case.gsub('_', ' ')
14
+ end
15
+ end
16
+ end
17
+ 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.0"
26
+ self.version = "0.3.1"
27
27
  self.author = "Erik Michaels-Ober"
28
28
 
29
29
  # Stub classes loaded hook - runs before LoadClasses BootLoader
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.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik Michaels-Ober
@@ -56,6 +56,8 @@ files:
56
56
  - README.markdown
57
57
  - Rakefile
58
58
  - lib/abstract_model.rb
59
+ - lib/datamapper_support.rb
60
+ - lib/generic_support.rb
59
61
  - lib/merb-admin
60
62
  - lib/merb-admin/merbtasks.rb
61
63
  - lib/merb-admin/slicetasks.rb