admin_data 1.0.21 → 1.0.22
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +4 -0
- data/app/controllers/admin_data/main_controller.rb +10 -14
- data/app/controllers/admin_data/public_controller.rb +14 -5
- data/app/controllers/admin_data/search_controller.rb +3 -3
- data/app/views/admin_data/main/association/_association_info.html.erb +1 -1
- data/app/views/admin_data/main/association/_belongs_to_info.html.erb +1 -1
- data/app/views/admin_data/main/association/_habtm_info.html.erb +1 -1
- data/app/views/admin_data/main/association/_has_many_info.html.erb +1 -1
- data/app/views/admin_data/main/association/_has_one_info.html.erb +1 -1
- data/app/views/admin_data/main/edit.html.erb +1 -1
- data/app/views/admin_data/main/{all_models.html.erb → index.html.erb} +4 -1
- data/app/views/admin_data/main/misc/_form.html.erb +1 -1
- data/app/views/admin_data/main/misc/_modify_record.html.erb +3 -3
- data/app/views/admin_data/main/new.html.erb +1 -1
- data/app/views/admin_data/search/search/_listing.html.erb +1 -1
- data/app/views/admin_data/shared/_header.html.erb +1 -1
- data/app/views/admin_data/shared/_secondary_navigation.html.erb +2 -2
- data/config/routes.rb +27 -23
- data/lib/admin_data/active_record_util.rb +62 -0
- data/lib/admin_data/chelper.rb +1 -1
- data/lib/admin_data/extension.rb +21 -0
- data/lib/admin_data/helpers.rb +43 -68
- data/lib/admin_data/util.rb +2 -58
- data/lib/admin_data/version.rb +1 -1
- data/lib/admin_data.rb +2 -0
- metadata +7 -5
data/Rakefile
CHANGED
@@ -8,7 +8,7 @@ class AdminData::MainController < AdminData::BaseController
|
|
8
8
|
|
9
9
|
before_filter :get_model_and_verify_it, :only => [:destroy, :del, :edit, :update, :show]
|
10
10
|
|
11
|
-
before_filter :ensure_is_allowed_to_view_klass, :except => [:
|
11
|
+
before_filter :ensure_is_allowed_to_view_klass, :except => [:index]
|
12
12
|
|
13
13
|
before_filter :ensure_is_allowed_to_update, :only => [:destroy, :del, :edit, :update, :create]
|
14
14
|
|
@@ -32,7 +32,7 @@ class AdminData::MainController < AdminData::BaseController
|
|
32
32
|
respond_to {|format| format.html}
|
33
33
|
end
|
34
34
|
|
35
|
-
def
|
35
|
+
def index
|
36
36
|
respond_to {|format| format.html}
|
37
37
|
end
|
38
38
|
|
@@ -69,12 +69,12 @@ class AdminData::MainController < AdminData::BaseController
|
|
69
69
|
model_name_underscored = @klass.name.underscore
|
70
70
|
model_attrs = update_model_with_assoc(params[model_name_underscored])
|
71
71
|
@columns = columns_list
|
72
|
-
|
72
|
+
|
73
73
|
respond_to do |format|
|
74
74
|
if @model.update_attributes(model_attrs)
|
75
75
|
format.html do
|
76
76
|
flash[:success] = "Record was updated"
|
77
|
-
redirect_to
|
77
|
+
redirect_to admin_data_path(:id => @model, :klass => @klass.name.underscore)
|
78
78
|
end
|
79
79
|
format.js { render :json => {:success => true}}
|
80
80
|
else
|
@@ -97,7 +97,7 @@ class AdminData::MainController < AdminData::BaseController
|
|
97
97
|
else
|
98
98
|
format.html do
|
99
99
|
flash[:success] = "Record was created"
|
100
|
-
redirect_to
|
100
|
+
redirect_to admin_data_path(:id => @model, :klass => @klass.name.underscore)
|
101
101
|
end
|
102
102
|
format.js { render :json => {} }
|
103
103
|
end
|
@@ -105,18 +105,14 @@ class AdminData::MainController < AdminData::BaseController
|
|
105
105
|
end
|
106
106
|
|
107
107
|
private
|
108
|
-
|
108
|
+
|
109
109
|
# If this class has any habtm relationships, update the parameters
|
110
110
|
# in the model with the actual objects so they can be saved properly
|
111
|
+
# TODO write test for it
|
111
112
|
def update_model_with_assoc(model_attrs)
|
112
|
-
|
113
|
-
|
114
|
-
|
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
|
113
|
+
AdminData::ActiveRecordUtil.habtm_klasses_for(klass).each do |k|
|
114
|
+
if model_attrs.include? k.table_name
|
115
|
+
model_attrs[k.table_name].map! { |s| k.find(s.to_i) }
|
120
116
|
end
|
121
117
|
end
|
122
118
|
model_attrs
|
@@ -3,16 +3,25 @@ class AdminData::PublicController < AdminData::BaseController
|
|
3
3
|
|
4
4
|
def serve
|
5
5
|
f = File.join(AdminData::Config.setting[:plugin_dir], 'lib', params[:file])
|
6
|
-
|
7
|
-
|
6
|
+
|
7
|
+
unless File.exists?(f)
|
8
|
+
render :nothing => true, :status => 404 and return
|
8
9
|
end
|
10
|
+
|
9
11
|
opts = {:text => File.new(f).read, :cache => true}
|
10
|
-
if f =~ /css$/
|
12
|
+
if f =~ /css$/
|
11
13
|
opts[:content_type] = "text/css"
|
12
|
-
elsif f =~ /js$/
|
14
|
+
elsif f =~ /js$/
|
13
15
|
opts[:content_type] = "text/javascript"
|
16
|
+
else
|
17
|
+
# this is needed so that a user is not able to get data by performing url like
|
18
|
+
# http://localhost:3000/admin_data/public/..%2F..%2F..%2F..%2F..%2F..%2F..%2F..%2Fetc%2Fpasswd
|
19
|
+
# Above url will try to get /etc/passwd
|
20
|
+
#
|
21
|
+
# ensures that request file must end with js or css
|
22
|
+
render :nothing => true, :status => 404 and return
|
14
23
|
end
|
15
24
|
render opts
|
16
25
|
end
|
17
26
|
|
18
|
-
end
|
27
|
+
end
|
@@ -79,11 +79,11 @@ 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
|
-
|
82
|
+
#TODO write test for this condition
|
83
|
+
if AdminData::ActiveRecordUtil.declared_has_many_association_names(model_klass).include?(params[:children]) || AdminData::ActiveRecordUtil.declared_habtm_association_names(model_klass).include?(params[:children])
|
83
84
|
#proceed
|
84
85
|
else
|
85
|
-
render :text => "#{params[:children]} is not a valid has_many association",
|
86
|
-
:status => :not_found
|
86
|
+
render :text => "#{params[:children]} is not a valid has_many association", :status => :not_found
|
87
87
|
return
|
88
88
|
end
|
89
89
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
<div class="block rounded">
|
2
2
|
<h3>Association Information</h3>
|
3
3
|
<div class='content association_info'>
|
4
|
-
<% if AdminData::
|
4
|
+
<% if AdminData::ActiveRecordUtil.association_info_size(klass) %>
|
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 %>
|
@@ -12,7 +12,7 @@
|
|
12
12
|
|
13
13
|
<div class="content rounded">
|
14
14
|
<div class="inner umbrella">
|
15
|
-
<% url =
|
15
|
+
<% url = admin_data_path(:klass => @klass.name, :id => @model)
|
16
16
|
html_options = {:class => 'form', :method => :put} %>
|
17
17
|
<%= form_for @model, :as => @klass.name.underscore.to_sym, :url => url, :html=> html_options do |f| %>
|
18
18
|
<%= render 'admin_data/shared/flash_message', :model => @model %>
|
@@ -1,13 +1,16 @@
|
|
1
1
|
<div id="main">
|
2
2
|
<div class="block" id="block-tables">
|
3
3
|
<div class="content rounded select_box">
|
4
|
-
<h2
|
4
|
+
<h2 id='main_page_label'>Select from the drop down menu above</h2>
|
5
5
|
<div class="clear"></div>
|
6
6
|
</div>
|
7
7
|
</div>
|
8
8
|
</div>
|
9
9
|
|
10
10
|
<style>
|
11
|
+
#main_page_label {
|
12
|
+
margin-left: 0;
|
13
|
+
}
|
11
14
|
.select_box {
|
12
15
|
padding: 15px;
|
13
16
|
}
|
@@ -8,7 +8,7 @@
|
|
8
8
|
</div>
|
9
9
|
<% end %>
|
10
10
|
|
11
|
-
<% if AdminData::
|
11
|
+
<% if AdminData::ActiveRecordUtil.declared_habtm_association_names(klass).any? %>
|
12
12
|
<div class='data'>
|
13
13
|
<%= admin_data_form_field_for_habtm_records(klass, @model, f, '').html_safe %>
|
14
14
|
</div>
|
@@ -3,14 +3,14 @@
|
|
3
3
|
<h3>Modify Record</h3>
|
4
4
|
<div style='padding-left:15px'>
|
5
5
|
<p>
|
6
|
-
<%= link_to 'Edit',
|
6
|
+
<%= link_to 'Edit', admin_data_edit_path(:klass => klass.name.underscore, :id => model) %>
|
7
7
|
</p>
|
8
8
|
<p>
|
9
|
-
<%= link_to 'Delete',
|
9
|
+
<%= link_to 'Delete', admin_data_del_path(:klass => klass.name.underscore, :id => model) ,
|
10
10
|
{ :confirm => ' You are deleting (not destroying) a record. Are you sure?', :method => :delete} %>
|
11
11
|
</p>
|
12
12
|
<p>
|
13
|
-
<%= link_to "Destroy",
|
13
|
+
<%= link_to "Destroy", admin_data_path(:klass => klass.name.underscore, :id => model),
|
14
14
|
{ :confirm => 'You are destroying a record. Are you sure?', :method => :delete} %>
|
15
15
|
</p>
|
16
16
|
</div>
|
@@ -14,7 +14,7 @@
|
|
14
14
|
<div class="inner umbrella">
|
15
15
|
<h1>Create a new record</h1>
|
16
16
|
<%= form_for @model, :as => @klass.name.underscore.to_sym,
|
17
|
-
:url =>
|
17
|
+
:url => admin_data_index_path(:klass => @klass.name),
|
18
18
|
:html => {:class => 'form', :method => :post} do |f| %>
|
19
19
|
<%= render 'admin_data/shared/flash_message', :model => @model %>
|
20
20
|
<%= render 'admin_data/main/misc/form', :klass => @klass, :f => f %>
|
@@ -23,7 +23,7 @@
|
|
23
23
|
<% AdminData::Util.columns_order(klass.name).each do |column| %>
|
24
24
|
<td>
|
25
25
|
<% if (column == klass.primary_key) %>
|
26
|
-
<%= link_to(record.send(column),
|
26
|
+
<%= link_to(record.send(column), admin_data_path(:klass => klass.name.underscore, :id => record)) %>
|
27
27
|
<% else %>
|
28
28
|
<%=h admin_data_get_value_for_column(admin_data_column_native(klass, column), record) %>
|
29
29
|
<% end %>
|
@@ -16,12 +16,12 @@
|
|
16
16
|
</li>
|
17
17
|
|
18
18
|
<li class="<%=@table_structure_tab_active%>">
|
19
|
-
<%= link_to 'Table Structure',
|
19
|
+
<%= link_to 'Table Structure', admin_data_table_structure_path(:klass => klass.name.underscore) %>
|
20
20
|
</li>
|
21
21
|
|
22
22
|
<% if admin_data_is_allowed_to_update? %>
|
23
23
|
<li class="<%=@add_new_record_tab_active%>">
|
24
|
-
<%= link_to '+Add New Record',
|
24
|
+
<%= link_to '+Add New Record', admin_data_new_path(:klass => klass.name.underscore) %>
|
25
25
|
</li>
|
26
26
|
<% end %>
|
27
27
|
|
data/config/routes.rb
CHANGED
@@ -1,31 +1,35 @@
|
|
1
1
|
Rails.application.routes.draw do
|
2
2
|
|
3
3
|
namespace(:admin_data) do
|
4
|
-
|
5
|
-
match '/' => "main#all_models", :as => :index
|
6
|
-
|
7
|
-
match '/migration' => "migration#index", :as => :migration_information
|
8
|
-
match '/jstest' => "migration#jstest", :as => :jstest
|
9
|
-
|
10
4
|
match '/feed/:klasss' => "feed#index", :defaults => { :format =>'rss' }, :as => :feed
|
11
|
-
|
12
|
-
match '/quick_search/:klass' => "search#quick_search", :as => :search
|
13
|
-
match '/advance_search/:klass' => "search#advance_search", :as => :advance_search
|
14
|
-
|
15
|
-
match '/public/*file' => "public#serve"
|
16
5
|
end
|
17
6
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
7
|
+
namespace(:admin_data) do
|
8
|
+
scope :admin_data do
|
9
|
+
|
10
|
+
controller "main" do
|
11
|
+
match '/', :to => :index, :as => :index
|
12
|
+
match '/klass/:klass/table_structure', :to => :table_structure, :as => :table_structure, :via => :get
|
13
|
+
match '/klass/(:klass)', :to => :index, :as => :index, :via => :get
|
14
|
+
match '/klass/(:klass)', :to => :create, :as => :index, :via => :post
|
15
|
+
match '/klass/:klass/new', :to => :new, :as => :new, :via => :get
|
16
|
+
match '/klass/:klass/:id/del', :to => :del, :as => :del, :via => :delete
|
17
|
+
match '/klass/:klass/:id/edit', :to => :edit, :as => :edit, :via => :get
|
18
|
+
match '/klass/:klass/:id', :to => :show, :via => :get
|
19
|
+
match '/klass/:klass/:id', :to => :update, :via => :put
|
20
|
+
match '/klass/:klass/:id', :to => :destroy, :via => :delete
|
21
|
+
end
|
22
|
+
|
23
|
+
controller "migration" do
|
24
|
+
match '/migration', :to => :index, :as => :migration_information
|
25
|
+
match '/jstest', :to => :jstest, :as => :jstest
|
26
|
+
end
|
27
|
+
|
28
|
+
match '/quick_search/:klass' => "search#quick_search", :as => :search
|
29
|
+
match '/advance_search/:klass' => "search#advance_search", :as => :advance_search
|
30
|
+
match '/public/*file' => "public#serve"
|
31
|
+
|
32
|
+
end
|
29
33
|
end
|
30
34
|
|
31
|
-
end
|
35
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module AdminData
|
2
|
+
class ActiveRecordUtil
|
3
|
+
|
4
|
+
def self.declared_habtm_association_names(klass)
|
5
|
+
delcared_association_names_for(klass, :has_and_belongs_to_many).map(&:name).map(&:to_s)
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.declared_belongs_to_association_names(klass)
|
9
|
+
delcared_association_names_for(klass, :belongs_to).map(&:name).map(&:to_s)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.declared_has_one_association_names(klass)
|
13
|
+
delcared_association_names_for(klass, :has_one).map(&:name).map(&:to_s)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.declared_has_many_association_names(klass)
|
17
|
+
delcared_association_names_for(klass, :has_many).map(&:name).map(&:to_s)
|
18
|
+
end
|
19
|
+
|
20
|
+
# returns declared association names like
|
21
|
+
# #=> [comments]
|
22
|
+
# #=> [positive_comments]
|
23
|
+
# #=> [negative_comments]
|
24
|
+
def self.delcared_association_names_for(klass, association_type)
|
25
|
+
klass.name.camelize.constantize.reflections.values.select do |value|
|
26
|
+
value.macro == association_type
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# returns an array of classes
|
31
|
+
# #=> [Comment]
|
32
|
+
def self.habtm_klasses_for(klass)
|
33
|
+
declared_habtm_association_names(klass).map do |assoc_name|
|
34
|
+
klass_for_association_type_and_name(klass, :has_and_belongs_to_many, assoc_name)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# returns a class or nil
|
39
|
+
#
|
40
|
+
# class User
|
41
|
+
# has_many :comments
|
42
|
+
# end
|
43
|
+
#
|
44
|
+
# AdminData::ActiveRecordUtil.habtm_klass_for_association_name(User, 'comments') #=> Comment
|
45
|
+
def self.klass_for_association_type_and_name(klass, association_type, association_name)
|
46
|
+
data = klass.name.camelize.constantize.reflections.values.detect do |value|
|
47
|
+
value.macro == association_type && value.name.to_s == association_name
|
48
|
+
end
|
49
|
+
data.klass if data # output of detect from hash is an array with key and value
|
50
|
+
end
|
51
|
+
# TODO test with polymorphic
|
52
|
+
|
53
|
+
|
54
|
+
def self.association_info_size(k)
|
55
|
+
AdminData::ActiveRecordUtil.declared_belongs_to_association_names(k).any? ||
|
56
|
+
AdminData::ActiveRecordUtil.declared_has_many_association_names(k).any? ||
|
57
|
+
AdminData::ActiveRecordUtil.declared_has_many_association_names(k).any? ||
|
58
|
+
AdminData::ActiveRecordUtil.declared_habtm_association_names(k).any?
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
data/lib/admin_data/chelper.rb
CHANGED
@@ -26,7 +26,7 @@ module AdminData::Chelper
|
|
26
26
|
|
27
27
|
def admin_data_invalid_record_link(klassu, id, error)
|
28
28
|
record = klassu.camelize.constantize.send(:find, id)
|
29
|
-
tmp =
|
29
|
+
tmp = admin_data_path(:klass => klasss.underscore, :id => record)
|
30
30
|
a = []
|
31
31
|
a << link_to(klasss, tmp, :target => '_blank')
|
32
32
|
a << id
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class AdminData::Extension
|
2
|
+
|
3
|
+
def self.show_info(model)
|
4
|
+
return []
|
5
|
+
klass = model.class
|
6
|
+
if klass == User
|
7
|
+
|
8
|
+
if (habtm_klasses = AdminData::ActiveRecordUtil.habtm_klasses_for(klass)).any?
|
9
|
+
habtm_klasses.each do |k|
|
10
|
+
name = k.columns.map(&:name).include?('name') ? :name : k.primary_key
|
11
|
+
data << [ k.table_name, model.send(k.table_name).map{ |e|
|
12
|
+
view.link_to(e.send(name), view.admin_data_path(:klass => k, :id => e.send(k.primary_key)))
|
13
|
+
}.join(", ").html_safe ]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end # end of method
|
19
|
+
|
20
|
+
|
21
|
+
end
|
data/lib/admin_data/helpers.rb
CHANGED
@@ -8,7 +8,7 @@ module AdminData::Helpers
|
|
8
8
|
output = []
|
9
9
|
if params[:base]
|
10
10
|
label = params[:base].camelize + ' ID ' + params[:model_id]
|
11
|
-
output << link_to(label,
|
11
|
+
output << link_to(label, admin_data_path(:klass => params[:base], :id => params[:model_id]))
|
12
12
|
output << 'has'
|
13
13
|
output << pluralize(total_num_of_children, params[:klass])
|
14
14
|
|
@@ -31,7 +31,7 @@ module AdminData::Helpers
|
|
31
31
|
|
32
32
|
def admin_data_invalid_record_link(klassu, id, error)
|
33
33
|
record = klassu.camelize.constantize.send(:find, id)
|
34
|
-
tmp =
|
34
|
+
tmp = admin_data_path(:klass => klassu, :id => record)
|
35
35
|
a = []
|
36
36
|
a << link_to(klassu.camelize, tmp, :target => '_blank')
|
37
37
|
a << id
|
@@ -40,13 +40,12 @@ module AdminData::Helpers
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def admin_data_has_one(model, klass)
|
43
|
-
tmp = AdminData::
|
43
|
+
tmp = AdminData::ActiveRecordUtil.declared_has_one_association_names(klass)
|
44
44
|
tmp.inject('') do |output, ho|
|
45
45
|
begin
|
46
46
|
label = ho
|
47
47
|
if model.send(ho)
|
48
|
-
|
49
|
-
output << link_to(label, admin_data_on_k_path(:klass => ho.underscore, :id => model.send(ho)))
|
48
|
+
output << link_to(label, admin_data_path(:klass => ho.underscore, :id => model.send(ho)))
|
50
49
|
else
|
51
50
|
output << label
|
52
51
|
end
|
@@ -58,17 +57,17 @@ module AdminData::Helpers
|
|
58
57
|
end
|
59
58
|
|
60
59
|
def admin_data_has_many_data(model, klass)
|
61
|
-
array = AdminData::
|
60
|
+
array = AdminData::ActiveRecordUtil.declared_has_many_association_names(klass).map do |m|
|
62
61
|
begin
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
62
|
+
count = model.send(m.intern).count
|
63
|
+
label = m.to_s + '(' + count.to_s + ')'
|
64
|
+
output = label
|
65
|
+
if count > 0
|
66
|
+
has_many_klass_name = AdminData::ActiveRecordUtil.klass_for_association_type_and_name(model.class, :has_many, m).name.underscore
|
67
|
+
output = link_to(label, admin_data_search_path( :klass => has_many_klass_name,
|
68
|
+
:children => m,
|
68
69
|
:base => klass.name.underscore,
|
69
70
|
:model_id => model.id))
|
70
|
-
else
|
71
|
-
output << label
|
72
71
|
end
|
73
72
|
rescue => e
|
74
73
|
Rails.logger.debug AdminData::Util.exception_info(e)
|
@@ -79,55 +78,45 @@ module AdminData::Helpers
|
|
79
78
|
end
|
80
79
|
|
81
80
|
def admin_data_belongs_to_data(model, klass)
|
82
|
-
|
81
|
+
AdminData::ActiveRecordUtil.declared_belongs_to_association_names(klass).map do |assoc_name|
|
83
82
|
begin
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
if belongs_to_record && t[:polymorphic]
|
89
|
-
output << link_to(belongs_to_record.class.name,
|
90
|
-
admin_data_on_k_path(:klass => belongs_to_record.class.name.underscore, :id => belongs_to_record))
|
91
|
-
elsif belongs_to_record
|
92
|
-
output << link_to(bt, admin_data_on_k_path(:klass => klass_name.underscore, :id => model.send(bt)))
|
93
|
-
else
|
94
|
-
output << bt
|
83
|
+
output = assoc_name
|
84
|
+
if belongs_to_record = model.send(assoc_name)
|
85
|
+
output = link_to(assoc_name, admin_data_path(:klass => belongs_to_record.class.name.underscore, :id => belongs_to_record.id))
|
95
86
|
end
|
96
87
|
rescue => e
|
97
88
|
Rails.logger.info AdminData::Util.exception_info(e)
|
98
89
|
end
|
99
90
|
output
|
100
|
-
end
|
101
|
-
array.join(', ')
|
91
|
+
end.join(', ')
|
102
92
|
end
|
103
|
-
|
93
|
+
|
104
94
|
def admin_data_habtm_data(model, klass)
|
105
|
-
|
106
|
-
# same as admin_data_has_many_data()
|
95
|
+
AdminData::ActiveRecordUtil.declared_habtm_association_names(klass).map do |assoc_name|
|
107
96
|
begin
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
97
|
+
count = model.send(assoc_name.intern).count
|
98
|
+
label = assoc_name + '(' + count.to_s + ')'
|
99
|
+
output = label
|
100
|
+
|
101
|
+
if count > 0 then
|
102
|
+
has_many_klass_name = AdminData::ActiveRecordUtil.klass_for_association_type_and_name(model.class, :has_and_belongs_to_many, assoc_name).name.underscore
|
103
|
+
output = link_to(label, admin_data_search_path( :klass => has_many_klass_name,
|
104
|
+
:children => assoc_name,
|
105
|
+
:base => klass.name.underscore,
|
106
|
+
:model_id => model.id))
|
117
107
|
end
|
118
108
|
rescue => e
|
119
109
|
Rails.logger.info AdminData::Util.exception_info(e)
|
120
110
|
end
|
121
111
|
output
|
122
|
-
end
|
123
|
-
array.join(', ')
|
112
|
+
end.join(', ')
|
124
113
|
end
|
125
|
-
|
114
|
+
|
126
115
|
def admin_data_habtm_values_for(model, klass)
|
127
|
-
assoc_klass = AdminData::
|
116
|
+
assoc_klass = AdminData::ActiveRecordUtil.klass_for_association_type_and_name(model, klass)
|
128
117
|
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),
|
118
|
+
model.send(assoc_klass.table_name).map{ |e|
|
119
|
+
link_to(e.send(name), admin_data_path(:klass => assoc_klass, :id => e.id))
|
131
120
|
}.join(", ").html_safe
|
132
121
|
end
|
133
122
|
|
@@ -174,24 +163,24 @@ module AdminData::Helpers
|
|
174
163
|
'could not retrieve' # returning nil
|
175
164
|
end
|
176
165
|
end
|
177
|
-
|
166
|
+
|
178
167
|
def admin_data_form_field_for_habtm_records(klass, model, f, html)
|
179
168
|
begin
|
180
169
|
html = []
|
181
|
-
AdminData::
|
170
|
+
AdminData::ActiveRecordUtil.delcared_habtm_association_names(klass).each do |k|
|
182
171
|
assoc_klass = AdminData::Util.get_class_name_for_habtm_association(model, k)
|
183
|
-
|
172
|
+
|
184
173
|
html << "<div class='col_box'>"
|
185
174
|
html << " <span class='col_name'>#{assoc_klass.table_name}</span>"
|
186
175
|
html << " <span class='col_type'>[integer]</span>"
|
187
176
|
html << "</div>"
|
188
|
-
|
177
|
+
|
189
178
|
order_by = assoc_klass.columns.map(&:name).include?('name') ? :name : assoc_klass.primary_key
|
190
179
|
all = assoc_klass.all(:order => order_by)
|
191
180
|
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
|
-
|
194
|
-
|
181
|
+
html << f.collection_select(assoc_klass.table_name, all, :id, order_by,
|
182
|
+
{:include_blank => false, :selected => selected},
|
183
|
+
{:multiple => true, :size => (all.count > 10 ? 8 : 4)})
|
195
184
|
end
|
196
185
|
html.join
|
197
186
|
rescue Exception => e
|
@@ -254,6 +243,7 @@ module AdminData::Helpers
|
|
254
243
|
# uses truncate method
|
255
244
|
# options supports :limit which is applied if the column type is string or text.
|
256
245
|
# calls the inspect method to convert to a string if the column is serialized.
|
246
|
+
# TODO rspec test limit option
|
257
247
|
def admin_data_get_value_for_column(column, model, options = {})
|
258
248
|
options.reverse_merge!(:limit => 400)
|
259
249
|
|
@@ -270,22 +260,7 @@ module AdminData::Helpers
|
|
270
260
|
'<actual data is not being shown because truncate method failed.>'
|
271
261
|
end
|
272
262
|
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
|
-
|
288
|
-
value
|
263
|
+
value.inspect
|
289
264
|
end
|
290
265
|
end
|
291
266
|
|
data/lib/admin_data/util.rb
CHANGED
@@ -53,7 +53,8 @@ class AdminData::Util
|
|
53
53
|
tmp = view.admin_data_get_value_for_column(column, model, :limit => nil)
|
54
54
|
sum << [ column.name, (tmp.html_safe? ? tmp : view.send(:h,tmp)) ]
|
55
55
|
end
|
56
|
-
|
56
|
+
extension = AdminData::Extension.show_info(model)
|
57
|
+
data + extension
|
57
58
|
end
|
58
59
|
|
59
60
|
def self.custom_value_for_column(column, model)
|
@@ -153,63 +154,6 @@ class AdminData::Util
|
|
153
154
|
data.join("\n")
|
154
155
|
end
|
155
156
|
|
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
|
162
|
-
end
|
163
|
-
|
164
|
-
def self.get_class_name_for_has_many_association(model, has_many_string)
|
165
|
-
data = model.class.name.camelize.constantize.reflections.values.detect do |value|
|
166
|
-
value.macro == :has_many && value.name.to_s == has_many_string
|
167
|
-
end
|
168
|
-
data.klass if data # output of detect from hash is an array with key and value
|
169
|
-
end
|
170
|
-
|
171
|
-
def self.get_class_name_for_belongs_to_class(model, belongs_to_string)
|
172
|
-
reflections = model.class.name.camelize.constantize.reflections
|
173
|
-
options = reflections.fetch(belongs_to_string.intern).send(:options)
|
174
|
-
return {:polymorphic => true} if options.keys.include?(:polymorphic) && options.fetch(:polymorphic)
|
175
|
-
{:klass_name => reflections[belongs_to_string.intern].klass.name }
|
176
|
-
end
|
177
|
-
|
178
|
-
def self.get_class_name_for_has_one_association(model, has_one_string)
|
179
|
-
data = model.class.name.camelize.constantize.reflections.values.detect do |value|
|
180
|
-
value.macro == :has_one && value.name.to_s == has_one_string
|
181
|
-
end
|
182
|
-
data.klass if data
|
183
|
-
end
|
184
|
-
|
185
|
-
def self.has_many_count(model, hm)
|
186
|
-
model.send(hm.intern).count
|
187
|
-
end
|
188
|
-
|
189
|
-
def self.has_many_what(klass)
|
190
|
-
associations_for(klass, :has_many).map(&:name).map(&:to_s)
|
191
|
-
end
|
192
|
-
|
193
|
-
def self.has_one_what(klass)
|
194
|
-
associations_for(klass, :has_one).map(&:name).map(&:to_s)
|
195
|
-
end
|
196
|
-
|
197
|
-
def self.belongs_to_what(klass)
|
198
|
-
associations_for(klass, :belongs_to).map(&:name).map(&:to_s)
|
199
|
-
end
|
200
|
-
|
201
|
-
def self.habtm_what(klass)
|
202
|
-
associations_for(klass, :has_and_belongs_to_many).map(&:name).map(&:to_s)
|
203
|
-
end
|
204
|
-
|
205
|
-
def self.habtm_count(model, m)
|
206
|
-
model.send(m.intern).count
|
207
|
-
end
|
208
|
-
|
209
|
-
def self.association_info_size(k)
|
210
|
-
belongs_to_what(k).any? || has_many_what(k).any? || has_one_what(k).any? || habtm_what(k).any?
|
211
|
-
end
|
212
|
-
|
213
157
|
def self.string_representation_of_data(value)
|
214
158
|
case value
|
215
159
|
when BigDecimal
|
data/lib/admin_data/version.rb
CHANGED
data/lib/admin_data.rb
CHANGED
@@ -15,7 +15,9 @@ require 'admin_data_date_validation'
|
|
15
15
|
require 'admin_data/helpers'
|
16
16
|
require 'admin_data/chelper'
|
17
17
|
require 'admin_data/settings'
|
18
|
+
require 'admin_data/extension'
|
18
19
|
require 'admin_data/util'
|
20
|
+
require 'admin_data/active_record_util'
|
19
21
|
|
20
22
|
AdminData::Config.initialize_defaults
|
21
23
|
ActionView::Base.send :include, AdminData::Helpers
|
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:
|
4
|
+
hash: 59
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 22
|
10
|
+
version: 1.0.22
|
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-
|
18
|
+
date: 2010-11-27 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -116,13 +116,13 @@ files:
|
|
116
116
|
- app/controllers/admin_data/public_controller.rb
|
117
117
|
- app/controllers/admin_data/search_controller.rb
|
118
118
|
- app/views/admin_data/feed/index.rss.builder
|
119
|
-
- app/views/admin_data/main/all_models.html.erb
|
120
119
|
- app/views/admin_data/main/association/_association_info.html.erb
|
121
120
|
- app/views/admin_data/main/association/_belongs_to_info.html.erb
|
122
121
|
- app/views/admin_data/main/association/_habtm_info.html.erb
|
123
122
|
- app/views/admin_data/main/association/_has_many_info.html.erb
|
124
123
|
- app/views/admin_data/main/association/_has_one_info.html.erb
|
125
124
|
- app/views/admin_data/main/edit.html.erb
|
125
|
+
- app/views/admin_data/main/index.html.erb
|
126
126
|
- app/views/admin_data/main/misc/_form.html.erb
|
127
127
|
- app/views/admin_data/main/misc/_modify_record.html.erb
|
128
128
|
- app/views/admin_data/main/new.html.erb
|
@@ -148,7 +148,9 @@ files:
|
|
148
148
|
- app/views/layouts/admin_data.html.erb
|
149
149
|
- config/routes.rb
|
150
150
|
- lib/admin_data.rb
|
151
|
+
- lib/admin_data/active_record_util.rb
|
151
152
|
- lib/admin_data/chelper.rb
|
153
|
+
- lib/admin_data/extension.rb
|
152
154
|
- lib/admin_data/helpers.rb
|
153
155
|
- lib/admin_data/railtie.rb
|
154
156
|
- lib/admin_data/search.rb
|