admin_data 1.0.19 → 1.0.21

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,6 @@
1
+ === 2010-11-08 v1.0.20
2
+ * support for habtm
3
+ * do not get the list of nested models. It breaks routing. Comprehensive nested models fix coming soon.
1
4
 
2
5
  === 2010-11-08 v1.0.19
3
6
  * used unscoped on the model to have basic search result always in the order user wants
@@ -64,7 +64,7 @@ class AdminData::BaseController < ApplicationController
64
64
  return $admin_data_all_klasses
65
65
  else
66
66
  model_dir = File.join(Rails.root, 'app', 'models')
67
- model_names = Dir.chdir(model_dir) { Dir["**/*.rb"] }
67
+ model_names = Dir.chdir(model_dir) { Dir["*.rb"] }
68
68
  klasses = get_klass_names(model_names)
69
69
  $admin_data_all_klasses = remove_klasses_without_table(klasses).sort_by {|r| r.name.underscore}
70
70
  end
@@ -67,8 +67,9 @@ class AdminData::MainController < AdminData::BaseController
67
67
 
68
68
  def update
69
69
  model_name_underscored = @klass.name.underscore
70
- model_attrs = params[model_name_underscored]
70
+ model_attrs = update_model_with_assoc(params[model_name_underscored])
71
71
  @columns = columns_list
72
+
72
73
  respond_to do |format|
73
74
  if @model.update_attributes(model_attrs)
74
75
  format.html do
@@ -85,7 +86,7 @@ class AdminData::MainController < AdminData::BaseController
85
86
 
86
87
  def create
87
88
  model_name_underscored = @klass.name.underscore
88
- model_attrs = params[model_name_underscored]
89
+ model_attrs = update_model_with_assoc(params[model_name_underscored])
89
90
  @model = @klass.create(model_attrs)
90
91
  @columns = columns_list
91
92
 
@@ -104,6 +105,22 @@ class AdminData::MainController < AdminData::BaseController
104
105
  end
105
106
 
106
107
  private
108
+
109
+ # If this class has any habtm relationships, update the parameters
110
+ # in the model with the actual objects so they can be saved properly
111
+ def update_model_with_assoc(model_attrs)
112
+ if AdminData::Util.habtm_what(@klass).any? then
113
+ AdminData::Util.habtm_what(klass).each do |k|
114
+ assoc_klass = AdminData::Util.get_class_name_for_habtm_association(@model || @klass, k)
115
+ if model_attrs.include? assoc_klass.table_name then
116
+ model_attrs[assoc_klass.table_name].map! do |s|
117
+ assoc_klass.find(s.to_i)
118
+ end
119
+ end
120
+ end
121
+ end
122
+ model_attrs
123
+ end
107
124
 
108
125
  def get_model_and_verify_it
109
126
  primary_key = @klass.primary_key.intern
@@ -0,0 +1,18 @@
1
+
2
+ class AdminData::PublicController < AdminData::BaseController
3
+
4
+ def serve
5
+ f = File.join(AdminData::Config.setting[:plugin_dir], 'lib', params[:file])
6
+ if not File.exists? f then
7
+ return render :nothing => true, :status => 404
8
+ end
9
+ opts = {:text => File.new(f).read, :cache => true}
10
+ if f =~ /css$/ then
11
+ opts[:content_type] = "text/css"
12
+ elsif f =~ /js$/ then
13
+ opts[:content_type] = "text/javascript"
14
+ end
15
+ render opts
16
+ end
17
+
18
+ end
@@ -79,8 +79,10 @@ class AdminData::SearchController < AdminData::BaseController
79
79
  render :text => "#{params[:base]} is an invalid value", :status => :not_found
80
80
  return
81
81
  end
82
- unless AdminData::Util.has_many_what(model_klass).include?(params[:children])
83
- render :text => "<h2>#{params[:children]} is not a valid has_many association</h2>",
82
+ if AdminData::Util.has_many_what(model_klass).include?(params[:children]) || AdminData::Util.habtm_what(model_klass).include?(params[:children])
83
+ #proceed
84
+ else
85
+ render :text => "#{params[:children]} is not a valid has_many association",
84
86
  :status => :not_found
85
87
  return
86
88
  end
@@ -5,6 +5,7 @@
5
5
  <%= render 'admin_data/main/association/belongs_to_info' , :klass => klass, :model => model %>
6
6
  <%= render 'admin_data/main/association/has_one_info', :klass => klass, :model => model %>
7
7
  <%= render 'admin_data/main/association/has_many_info' , :klass => klass, :model => model %>
8
+ <%= render 'admin_data/main/association/habtm_info' , :klass => klass, :model => model %>
8
9
  <% end %>
9
10
  </div>
10
11
  </div>
@@ -0,0 +1,7 @@
1
+ <% if AdminData::Util.habtm_what(klass).any? %>
2
+ <p>
3
+ <strong>has_and_belongs_to_many :</strong>
4
+ <%= admin_data_habtm_data(model, klass).html_safe %>
5
+ &nbsp;
6
+ </p>
7
+ <% end %>
@@ -8,6 +8,12 @@
8
8
  </div>
9
9
  <% end %>
10
10
 
11
+ <% if AdminData::Util.habtm_what(klass).any? %>
12
+ <div class='data'>
13
+ <%= admin_data_form_field_for_habtm_records(klass, @model, f, '').html_safe %>
14
+ </div>
15
+ <% end %>
16
+
11
17
  <div class='group navform' style='padding-top:10px;'>
12
18
  <% if params[:action] == 'edit' || params[:action] == 'update' %>
13
19
  <% label = 'Update' %>
data/config/routes.rb CHANGED
@@ -11,6 +11,8 @@ Rails.application.routes.draw do
11
11
 
12
12
  match '/quick_search/:klass' => "search#quick_search", :as => :search
13
13
  match '/advance_search/:klass' => "search#advance_search", :as => :advance_search
14
+
15
+ match '/public/*file' => "public#serve"
14
16
  end
15
17
 
16
18
  scope '/admin_data' do
@@ -100,6 +100,36 @@ module AdminData::Helpers
100
100
  end
101
101
  array.join(', ')
102
102
  end
103
+
104
+ def admin_data_habtm_data(model, klass)
105
+ array = AdminData::Util.habtm_what(klass).inject([]) do |output, m|
106
+ # same as admin_data_has_many_data()
107
+ begin
108
+ label = m + '(' + AdminData::Util.habtm_count(model, m).to_s + ')'
109
+ if AdminData::Util.habtm_count(model, m) > 0 then
110
+ has_many_klass_name = AdminData::Util.get_class_name_for_habtm_association(model,m).name.underscore
111
+ output << link_to(label, admin_data_search_path( :klass => has_many_klass_name,
112
+ :children => m,
113
+ :base => klass.name.underscore,
114
+ :model_id => model.id))
115
+ else
116
+ output << label
117
+ end
118
+ rescue => e
119
+ Rails.logger.info AdminData::Util.exception_info(e)
120
+ end
121
+ output
122
+ end
123
+ array.join(', ')
124
+ end
125
+
126
+ def admin_data_habtm_values_for(model, klass)
127
+ assoc_klass = AdminData::Util.get_class_name_for_habtm_association(model, klass)
128
+ name = assoc_klass.columns.map(&:name).include?('name') ? :name : assoc_klass.primary_key
129
+ model.send(assoc_klass.table_name).map{ |e|
130
+ link_to(e.send(name), admin_data_on_k_path(:klass => assoc_klass, :id => e.id))
131
+ }.join(", ").html_safe
132
+ end
103
133
 
104
134
  def admin_data_breadcrum(&block)
105
135
  render(:partial => '/admin_data/shared/breadcrum', :locals => {:data => capture(&block)})
@@ -144,6 +174,31 @@ module AdminData::Helpers
144
174
  'could not retrieve' # returning nil
145
175
  end
146
176
  end
177
+
178
+ def admin_data_form_field_for_habtm_records(klass, model, f, html)
179
+ begin
180
+ html = []
181
+ AdminData::Util.habtm_what(klass).each do |k|
182
+ assoc_klass = AdminData::Util.get_class_name_for_habtm_association(model, k)
183
+
184
+ html << "<div class='col_box'>"
185
+ html << " <span class='col_name'>#{assoc_klass.table_name}</span>"
186
+ html << " <span class='col_type'>[integer]</span>"
187
+ html << "</div>"
188
+
189
+ order_by = assoc_klass.columns.map(&:name).include?('name') ? :name : assoc_klass.primary_key
190
+ all = assoc_klass.all(:order => order_by)
191
+ selected = model.send(assoc_klass.table_name).map{|e| e.id}
192
+ html << f.collection_select(assoc_klass.table_name, all, :id, order_by,
193
+ {:include_blank => false, :selected => selected},
194
+ {:multiple => true, :size => (all.count > 10 ? 8 : 4)})
195
+ end
196
+ html.join
197
+ rescue Exception => e
198
+ Rails.logger.info AdminData::Util.exception_info(e)
199
+ 'could not retrieve' # returning nil
200
+ end
201
+ end
147
202
 
148
203
  def admin_data_handle_column_type(col, html, model, column_value, f)
149
204
  case col.type
@@ -215,6 +270,21 @@ module AdminData::Helpers
215
270
  '<actual data is not being shown because truncate method failed.>'
216
271
  end
217
272
  else
273
+
274
+ # check for an associated class id and add it's name to the value
275
+ ar = model.class.reflections.values.detect{ |v| v.primary_key_name == column.name}
276
+ if not ar.nil? then
277
+ name = ar.klass.columns.map(&:name).include?('name') ? :name : ar.klass.primary_key
278
+ assoc = model.send(ar.name)
279
+ if not name.nil? then
280
+ value = ("#{value} (" +
281
+ link_to(
282
+ assoc.send(name),
283
+ admin_data_on_k_path(:klass => ar.klass,
284
+ :id => assoc.send(ar.klass.primary_key))) + ")").html_safe
285
+ end
286
+ end
287
+
218
288
  value
219
289
  end
220
290
  end
@@ -49,10 +49,11 @@ class AdminData::Util
49
49
 
50
50
 
51
51
  def self.label_values_pair_for(model, view)
52
- model.class.columns.inject([]) do |sum, column|
52
+ data = model.class.columns.inject([]) do |sum, column|
53
53
  tmp = view.admin_data_get_value_for_column(column, model, :limit => nil)
54
- sum << [column.name, view.send(:h,tmp)]
54
+ sum << [ column.name, (tmp.html_safe? ? tmp : view.send(:h,tmp)) ]
55
55
  end
56
+ data
56
57
  end
57
58
 
58
59
  def self.custom_value_for_column(column, model)
@@ -137,19 +138,27 @@ class AdminData::Util
137
138
  end
138
139
 
139
140
  def self.javascript_include_tag(*args)
140
- data = args.inject('') do |sum, arg|
141
- f = File.new(File.join(AdminData::Config.setting[:plugin_dir], 'lib', 'js', "#{arg}.js"))
142
- sum << f.read
141
+ data = args.inject([]) do |sum, arg|
142
+ arg = "#{arg}.js" if arg !~ /js$/
143
+ sum << ['<script type="text/javascript" src="/admin_data/public/js/', arg, '"></script>'].join
143
144
  end
144
- ['<script type="text/javascript">', data, '</script>'].join
145
+ data.join("\n")
145
146
  end
146
147
 
147
148
  def self.stylesheet_link_tag(*args)
148
- data = args.inject('') do |sum, arg|
149
- f = File.new(File.join(AdminData::Config.setting[:plugin_dir], 'lib', 'css', "#{arg}.css"))
150
- sum << f.read
149
+ data = args.inject([]) do |sum, arg|
150
+ arg = "#{arg}.css" if arg !~ /css$/
151
+ sum << ['<link href="/admin_data/public/css/', arg, '" media="screen" rel="stylesheet" type="text/css" />'].join
151
152
  end
152
- ["<style type='text/css'>", data, '</style>'].join
153
+ data.join("\n")
154
+ end
155
+
156
+ def self.get_class_name_for_habtm_association(model, has_many_string)
157
+ klass = model.kind_of?(Class) ? model : model.class
158
+ data = klass.name.camelize.constantize.reflections.values.detect do |value|
159
+ value.macro == :has_and_belongs_to_many && value.name.to_s == has_many_string
160
+ end
161
+ data.klass if data # output of detect from hash is an array with key and value
153
162
  end
154
163
 
155
164
  def self.get_class_name_for_has_many_association(model, has_many_string)
@@ -192,6 +201,10 @@ class AdminData::Util
192
201
  def self.habtm_what(klass)
193
202
  associations_for(klass, :has_and_belongs_to_many).map(&:name).map(&:to_s)
194
203
  end
204
+
205
+ def self.habtm_count(model, m)
206
+ model.send(m.intern).count
207
+ end
195
208
 
196
209
  def self.association_info_size(k)
197
210
  belongs_to_what(k).any? || has_many_what(k).any? || has_one_what(k).any? || habtm_what(k).any?
@@ -1,3 +1,3 @@
1
1
  module AdminData
2
- VERSION = '1.0.19'
2
+ VERSION = '1.0.21'
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: admin_data
3
3
  version: !ruby/object:Gem::Version
4
- hash: 49
4
+ hash: 61
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 19
10
- version: 1.0.19
9
+ - 21
10
+ version: 1.0.21
11
11
  platform: ruby
12
12
  authors:
13
13
  - Neeraj Singh
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-17 00:00:00 -05:00
18
+ date: 2010-11-19 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -113,11 +113,13 @@ files:
113
113
  - app/controllers/admin_data/feed_controller.rb
114
114
  - app/controllers/admin_data/main_controller.rb
115
115
  - app/controllers/admin_data/migration_controller.rb
116
+ - app/controllers/admin_data/public_controller.rb
116
117
  - app/controllers/admin_data/search_controller.rb
117
118
  - app/views/admin_data/feed/index.rss.builder
118
119
  - app/views/admin_data/main/all_models.html.erb
119
120
  - app/views/admin_data/main/association/_association_info.html.erb
120
121
  - app/views/admin_data/main/association/_belongs_to_info.html.erb
122
+ - app/views/admin_data/main/association/_habtm_info.html.erb
121
123
  - app/views/admin_data/main/association/_has_many_info.html.erb
122
124
  - app/views/admin_data/main/association/_has_one_info.html.erb
123
125
  - app/views/admin_data/main/edit.html.erb