kitsune 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rake'
2
2
 
3
3
  gem_spec = Gem::Specification.new do |gem_spec|
4
4
  gem_spec.name = "kitsune"
5
- gem_spec.version = "0.0.5"
5
+ gem_spec.version = "0.0.6"
6
6
  gem_spec.summary = "Integrated Rails Content Management System."
7
7
  gem_spec.email = "matt@toastyapps.com"
8
8
  gem_spec.homepage = "http://github.com/toastyapps/kitsune"
@@ -1,21 +1,3 @@
1
1
  class Admin::Kitsune::ApplicationController < ActionController::Base
2
- before_filter :load_models
3
-
4
- private
5
- def load_models
6
- models = Kitsune.models_with_admin.map{ |m| Kitsune::Inspector.new(m) }
7
- category_map = {}
8
-
9
- @models =[]
10
- models.each do |m|
11
- if m.category
12
- category_map[m.category] = [] unless category_map[m.category]
13
- category_map[m.category] << m
14
- else
15
- @models << m
16
- end
17
- end
18
- @models += category_map.to_a.map{|c| Hash[*c]}
19
- #@models = models
20
- end
2
+ # this class should only be overridden
21
3
  end
@@ -0,0 +1,21 @@
1
+ class Admin::Kitsune::KitsuneController < Admin::Kitsune::ApplicationController
2
+ before_filter :load_models
3
+
4
+ private
5
+ def load_models
6
+ models = Kitsune.models_with_admin.map{ |m| Kitsune::Inspector.new(m) }
7
+ category_map = {}
8
+
9
+ @models =[]
10
+ models.each do |m|
11
+ if m.category
12
+ category_map[m.category] = [] unless category_map[m.category]
13
+ category_map[m.category] << m
14
+ else
15
+ @models << m
16
+ end
17
+ end
18
+ @models += category_map.to_a.map{|c| Hash[*c]}
19
+ #@models = models
20
+ end
21
+ end
@@ -1,4 +1,4 @@
1
- class Admin::Kitsune::ModelsController < Admin::Kitsune::ApplicationController
1
+ class Admin::Kitsune::ModelsController < Admin::Kitsune::KitsuneController
2
2
  layout 'admin/kitsune'
3
3
 
4
4
  before_filter :load_model
@@ -1,4 +1,4 @@
1
- class Admin::Kitsune::PagesController < Admin::Kitsune::ApplicationController
1
+ class Admin::Kitsune::PagesController < Admin::Kitsune::KitsuneController
2
2
  layout 'admin/kitsune'
3
3
 
4
4
  before_filter :load_page
@@ -14,12 +14,20 @@ class Admin::Kitsune::PagesController < Admin::Kitsune::ApplicationController
14
14
  def create
15
15
  @page = ::Page.new(params[:page])
16
16
  if @page.save
17
- redirect_to admin_kitsune_pages_path
17
+ redirect_to url_for(:controller => 'admin/kitsune/pages')
18
18
  else
19
19
  render 'new'
20
20
  end
21
21
  end
22
22
 
23
+ def update
24
+ if @page.update_attributes(params[:page])
25
+ redirect_to url_for(:controller => 'admin/kitsune/pages')
26
+ else
27
+ render 'edit'
28
+ end
29
+ end
30
+
23
31
  private
24
32
  def load_page
25
33
  @page = Page.find_by_url(params[:id]) if params[:id]
@@ -1,4 +1,4 @@
1
- class Admin::Kitsune::RecordsController < Admin::Kitsune::ApplicationController
1
+ class Admin::Kitsune::RecordsController < Admin::Kitsune::KitsuneController
2
2
  layout 'admin/kitsune'
3
3
 
4
4
  before_filter :load_model
@@ -27,7 +27,13 @@ class Admin::Kitsune::RecordsController < Admin::Kitsune::ApplicationController
27
27
  end
28
28
  if @record.save
29
29
  flash[:notice] = "Record Saved"
30
- redirect_to admin_kitsune_model_records_path(params[:model_id])
30
+ if params[:redirect]
31
+ klass = params[:redirect].gsub(/_id$/, '').classify.constantize
32
+ record = klass.find(params[:redirect_id])
33
+ redirect_to url_for(:controller => 'admin/kitsune/records', :model_id => klass.to_s, :id => params[:redirect_id], :action => :edit)
34
+ else
35
+ redirect_to url_for(:controller => 'admin/kitsune/records', :model_id => @model.class_name)
36
+ end
31
37
  else
32
38
  flash[:notice] = "Could not save record"
33
39
  render 'new'
@@ -35,9 +41,23 @@ class Admin::Kitsune::RecordsController < Admin::Kitsune::ApplicationController
35
41
  end
36
42
 
37
43
  def update
44
+
45
+ if @model.is_sti_child? && params[params[:model_id].underscore][:type]
46
+ if params[params[:model_id].underscore][:type].to_s != @record.type.to_s
47
+ @record.type = params[params[:model_id].underscore][:type]
48
+ @record.save
49
+ end
50
+ end
51
+
38
52
  if @record.update_attributes(params[params[:model_id].underscore])
39
53
  flash[:notice] = "Record Saved"
40
- redirect_to admin_kitsune_model_records_path(params[:model_id])
54
+ if params[:redirect]
55
+ klass = params[:redirect].gsub(/_id$/, '').classify.constantize
56
+ record = klass.find(params[:redirect_id])
57
+ redirect_to url_for(:controller => 'admin/kitsune/records', :model_id => klass.to_s, :id => params[:redirect_id], :action => :edit)
58
+ else
59
+ redirect_to url_for(:controller => 'admin/kitsune/records', :model_id => @model.class_name)
60
+ end
41
61
  else
42
62
  flash[:notice] = "Could not save record"
43
63
  render 'edit'
@@ -47,7 +67,13 @@ class Admin::Kitsune::RecordsController < Admin::Kitsune::ApplicationController
47
67
  def destroy
48
68
  @record.destroy
49
69
  flash[:notice] = "Record Deleted"
50
- redirect_to admin_kitsune_model_records_path(params[:model_id])
70
+ if params[:redirect]
71
+ klass = params[:redirect].gsub(/_id$/, '').classify.constantize
72
+ record = klass.find(params[:redirect_id])
73
+ redirect_to url_for(:controller => 'admin/kitsune/records', :model_id => klass.to_s, :id => params[:redirect_id], :action => :edit)
74
+ else
75
+ redirect_to url_for(:controller => 'admin/kitsune/records', :model_id => @model.class_name)
76
+ end
51
77
  end
52
78
 
53
79
  private
@@ -57,4 +83,4 @@ class Admin::Kitsune::RecordsController < Admin::Kitsune::ApplicationController
57
83
  def load_record
58
84
  @record = @model.find(params[:id]) if params[:id]
59
85
  end
60
- end
86
+ end
@@ -1,5 +1,5 @@
1
1
  class KitsuneController < ApplicationController
2
2
  def show
3
- @page = ::Page.find_by_url(params[:url].join('/'))
3
+ @page = ::Page.find_by_url([params[:url]].flatten.join('/'))
4
4
  end
5
5
  end
@@ -16,4 +16,12 @@ module Admin::Kitsune::RecordsHelper
16
16
  column.name.to_s.titleize
17
17
  end
18
18
  end
19
- end
19
+
20
+ def additional_content_for(model, record, column)
21
+ if model.field_type(column.name) == :file_field && (file = record.send(column.name)).present?
22
+ "<p><strong>Current File</strong>: " + link_to(file.to_s, file.to_s) + "</p>"
23
+ elsif model.field_type(column.name) == :image_field && (file = record.send(column.name)).present?
24
+ "<p><strong>Current File</strong>: " + image_tag(file.to_s) + "</p>"
25
+ end
26
+ end
27
+ end
@@ -1,5 +1,5 @@
1
1
  <div id='menu'>
2
- <%= link_to 'Back', admin_kitsune_pages_path %>
2
+ <%= link_to 'Back', :controller => 'admin/kitsune/pages' %>
3
3
  </div>
4
4
  <div id='body'>
5
5
  <h2 class='legend'>Edit Page</h3>
@@ -2,4 +2,4 @@
2
2
  <%= page.title %>
3
3
  <% end %>
4
4
 
5
- <%= link_to 'New Page', new_admin_kitsune_page_path %>
5
+ <%= link_to 'New Page', :controller => 'admin/kitsune/pages', :action => :new %>
@@ -1,5 +1,5 @@
1
1
  <div id='menu'>
2
- <%= link_to 'Back', admin_kitsune_pages_path %>
2
+ <%= link_to 'Back', :controller => 'admin/kitsune/pages' %>
3
3
  </div>
4
4
  <div id='body'>
5
5
  <h2 class='legend'>New Page</h3>
@@ -3,12 +3,19 @@
3
3
  <%= form.error_messages %>
4
4
  <% @model.columns(:edit).each do |column| %>
5
5
  <div class='field'>
6
- <%= form.label column.name %>
7
- <%= form.send(@model.form_type(column), column.name, *@model.options_for(column)) %>
6
+ <% unless params[column.name] %>
7
+ <%= form.label column.name %>
8
+ <%= form.send(@model.form_type(column), column.name, *@model.options_for(column)) %>
9
+ <%= additional_content_for(@model, @record, column) %>
10
+ <% else %>
11
+ <%= form.hidden_field column.name, :value => params[column.name] %>
12
+ <%= hidden_field_tag :redirect, column.name %>
13
+ <%= hidden_field_tag :redirect_id, params[column.name] %>
14
+ <% end %>
8
15
  </div>
9
16
  <% end %>
10
17
  <div class="submit_button">
11
18
  <button value="Save" class="submitBtn"><span>Save</span></button>
12
19
  </div>
13
20
  </fieldset>
14
- <% end %>
21
+ <% end %>
@@ -0,0 +1,32 @@
1
+ <table>
2
+ <thead>
3
+ <tr>
4
+ <% model.columns(:display).each do |column| %>
5
+ <th><%= sort_link_to(model, column) %></th>
6
+ <% end %>
7
+ <th>Actions</th>
8
+ </tr>
9
+ </thead>
10
+ <tbody>
11
+ <% records.each do |record| %>
12
+ <tr class="<%= cycle('odd', 'even') %>">
13
+ <% model.columns(:display).each do |column| %>
14
+ <td><%= truncate(sanitize(model.display_for(record, column.name).to_s), :length => 20) %></td>
15
+ <% end %>
16
+ <td>
17
+ <% if parent %>
18
+ <%= link_to 'Edit', :controller => 'admin/kitsune/records', :action => :edit, :model_id => model.object_class, :id => record.id, (parent.class.to_s.underscore+'_id').to_sym => parent.id %>
19
+ <% else %>
20
+ <%= link_to 'Edit', :controller => 'admin/kitsune/records', :action => :edit, :model_id => model.object_class, :id => record.id %>
21
+ <% end %>
22
+ -
23
+ <% if parent %>
24
+ <%= link_to('Delete', url_for(:controller => 'admin/kitsune/records', :model_id => model.object_class, :id => record.id, :action => :destroy, :redirect => (parent.class.to_s.underscore+'_id'), :redirect_id => parent.id), :method => :delete, :confirm => "Are you sure?") %>
25
+ <% else %>
26
+ <%= link_to('Delete', url_for(:controller => 'admin/kitsune/records', :model_id => model.object_class, :id => record.id, :action => :destroy), :method => :delete, :confirm => "Are you sure?") %>
27
+ <% end %>
28
+ </td>
29
+ </tr>
30
+ <% end %>
31
+ </tbody>
32
+ </table>
@@ -1,7 +1,15 @@
1
1
  <div id='kitsune_record_header'>
2
2
  <h2 class='legend'>Edit <%= params[:model_id] %></h2>
3
3
  <div id='menu'>
4
- <%= link_to 'Back', admin_kitsune_model_records_path(params[:model_id]) %>
4
+ <%= link_to 'Back', :controller => 'admin/kitsune/records', :model_id => params[:model_id] %>
5
5
  </div>
6
6
  </div>
7
- <%= render 'form' %>
7
+ <%= render 'form' %>
8
+
9
+ <% @model.columns(:reflections).each do |column| %>
10
+ <div class='reflection'>
11
+ <h3><%= column.name.to_s.titleize %></h3>
12
+ <%= link_to 'New', :controller => 'admin/kitsune/records', :action => :new, :model_id => column.name.to_s.classify, (@record.class.to_s.underscore+'_id').to_sym => @record.id %>
13
+ <%= render :partial => 'list', :locals => { :model => Kitsune::Inspector.new(column.name.to_s.classify.constantize), :records => @record.send(column.name), :parent => @record } %>
14
+ </div>
15
+ <% end %>
@@ -1,35 +1,11 @@
1
1
  <div id='kitsune_record_header'>
2
2
  <h2><%= @model.admin_name %></h2>
3
3
  <div id='menu'>
4
- <%= link_to "New #{params[:id]}", new_admin_kitsune_model_record_path(params[:model_id]) %>
4
+ <%= link_to "New #{params[:id]}", :controller => 'admin/kitsune/records', :action => :new, :model_id => params[:model_id] %>
5
5
  </div>
6
6
  </div>
7
7
  <% @model.tabs.each do |key, value|%>
8
8
  <%= link_to key, value %>
9
9
  <% end %>
10
- <table>
11
- <thead>
12
- <tr>
13
- <% @model.columns(:display).each do |column| %>
14
- <th><%= sort_link_to(@model, column) %></th>
15
- <% end %>
16
- <th>Actions</th>
17
- </tr>
18
- </thead>
19
- <tbody>
20
- <% @records.each do |record| %>
21
- <tr class="<%= cycle('odd', 'even') %>">
22
- <% @model.columns(:display).each do |column| %>
23
- <td><%= truncate(sanitize(record.send(column.name).to_s), :length => 20) %></td>
24
- <% end %>
25
- <td>
26
- <%= link_to 'Edit', edit_admin_kitsune_model_record_path(params[:model_id], record.id) %>
27
- -
28
- <%= link_to 'Delete', admin_kitsune_model_record_path(params[:model_id], record.id), :method => :delete, :confirm => "Are you sure?" %>
29
- </td>
30
- </tr>
31
- <% end %>
32
- </tbody>
33
- </table>
34
-
35
- <%= will_paginate @records %>
10
+ <%= render :partial => 'list', :locals => {:model => @model, :records => @records, :parent => nil } %>
11
+ <%= will_paginate @records %>
@@ -1,7 +1,7 @@
1
1
  <div id='kitsune_record_header'>
2
2
  <h2 class='legend'>New <%= params[:model_id] %></h2>
3
3
  <div id='menu'>
4
- <%= link_to 'Back', admin_kitsune_model_records_path(params[:model_id]) %>
4
+ <%= link_to 'Back', :controller => 'admin/kitsune/records', :model_id => params[:model_id] %>
5
5
  </div>
6
6
  </div>
7
7
 
@@ -18,12 +18,12 @@
18
18
  <div class='kitsune_category'>
19
19
  <h3><%= key %></h3>
20
20
  <% value.each do |inner_model| %>
21
- <div class='kitsune_inner_model_link'><%= link_to inner_model.admin_name, admin_kitsune_model_records_path(inner_model.object_class) %></div>
21
+ <div class='kitsune_inner_model_link'><%= link_to inner_model.admin_name, :controller => 'admin/kitsune/records', :model_id => inner_model.object_class %></div>
22
22
  <% end %>
23
23
  </div>
24
24
  <% end %>
25
25
  <% else %>
26
- <div class='kitsune_model_link'><%= link_to model.admin_name, admin_kitsune_model_records_path(model.object_class) %></div>
26
+ <div class='kitsune_model_link'><%= link_to model.admin_name, :controller => 'admin/kitsune/records', :model_id => model.object_class %></div>
27
27
  <% end %>
28
28
  <% end %>
29
29
  </div>
@@ -3,9 +3,10 @@ ActionController::Routing::Routes.draw do |map|
3
3
  map.namespace :admin do |admin|
4
4
  admin.namespace :kitsune do |kitsune|
5
5
  kitsune.resources :pages
6
- kitsune.resources :models, :has_many => [:records]
6
+ kitsune.resources :models do |model|
7
+ model.resources :records
8
+ end
7
9
  end
8
10
  end
9
-
10
11
  map.connect '/*url', :controller => 'kitsune', :action => 'show'
11
- end
12
+ end
@@ -10,6 +10,7 @@ module Kitsune
10
10
  :multipart => false,
11
11
  :display => {},
12
12
  :edit => {},
13
+ :reflections => {},
13
14
  :fields => {},
14
15
  :tabs => {},
15
16
  :is_sti => false
@@ -49,7 +49,7 @@ module Kitsune
49
49
  end
50
50
 
51
51
  def image(field)
52
- add :file_field, field
52
+ add :image_field, field
53
53
  multipart
54
54
  end
55
55
 
@@ -81,7 +81,10 @@ module Kitsune
81
81
  end
82
82
 
83
83
  def edit(*fields)
84
- set :edit, fields
84
+ fields.each do |field|
85
+ type = @resource.reflections[field.to_sym] ? :reflections : :edit
86
+ set type, field
87
+ end
85
88
  end
86
89
 
87
90
  def no_edit(*fields)
@@ -103,7 +106,7 @@ module Kitsune
103
106
  def set(type, fields)
104
107
  @resource.kitsune_admin[type] = {} unless @resource.kitsune_admin[type]
105
108
  @resource.kitsune_admin[type][:fields] ||= []
106
- fields.each do |field|
109
+ [fields].flatten.each do |field|
107
110
  unless field.is_a?(Hash)
108
111
  @resource.kitsune_admin[type][:fields] << field
109
112
  else
@@ -112,4 +115,4 @@ module Kitsune
112
115
  end
113
116
  end
114
117
  end
115
- end
118
+ end
@@ -21,6 +21,10 @@ module Kitsune
21
21
  kitsune_admin[:is_sti]
22
22
  end
23
23
 
24
+ def is_sti_child?
25
+ @object.ancestors[1] != ::ActiveRecord::Base
26
+ end
27
+
24
28
  def sti_column
25
29
  kitsune_admin[:sti_column]
26
30
  end
@@ -72,6 +76,16 @@ module Kitsune
72
76
  end
73
77
  end
74
78
 
79
+ def columns_for_reflections
80
+ if kitsune_admin[:reflections] && kitsune_admin[:reflections][:fields]
81
+ kitsune_admin[:reflections][:fields].map do |field|
82
+ Kitsune::FauxColumn.new(field, :string)
83
+ end
84
+ else
85
+ []
86
+ end
87
+ end
88
+
75
89
  def column_sortable(column)
76
90
  # move to column proxy
77
91
  kitsune_admin[:sortable] && kitsune_admin[:sortable].include?(column.name.to_sym)
@@ -82,14 +96,17 @@ module Kitsune
82
96
  end
83
97
 
84
98
  def form_type(column)
99
+ field = column.name
85
100
  if type = field_type(column.name)
86
101
  case type
87
102
  when :sti
88
- if field_options(field) && field[:classes]
103
+ if (options = field_options(field)) && options[:classes]
89
104
  :select
90
105
  else
91
106
  :text_field
92
107
  end
108
+ when :image_field
109
+ :file_field
93
110
  else
94
111
  kitsune_admin[:fields][column.name.to_sym][:type]
95
112
  end
@@ -109,7 +126,7 @@ module Kitsune
109
126
 
110
127
  def detect_label(collection)
111
128
  @collection_label_methods.each do |label_name|
112
- return label_name if collection.first.respond_to?(label_name)
129
+ return label_name if [collection].flatten.first.respond_to?(label_name)
113
130
  end
114
131
  'to_s'
115
132
  end
@@ -135,6 +152,16 @@ module Kitsune
135
152
  end
136
153
  end
137
154
 
155
+ def display_for(record, method, *args, &block)
156
+ if method.to_s =~ /_id$/
157
+ associated_record = record.send(method.to_s.gsub(/_id$/, '').to_sym, *args, &block)
158
+ label_method = detect_label(associated_record)
159
+ associated_record.send(label_method.to_sym)
160
+ else
161
+ record.send(method.to_sym, *args, &block)
162
+ end
163
+ end
164
+
138
165
  def form_options_for(record)
139
166
  options = {:method => method_for_record(record)}
140
167
  if kitsune_admin[:multipart]
@@ -167,15 +194,15 @@ module Kitsune
167
194
  end
168
195
  end
169
196
 
170
- private
171
- def field_defined(field)
172
- !!kitsune_admin[:fields][field.to_sym]
173
- end
174
-
175
197
  def field_type(field)
176
198
  return kitsune_admin[:fields][field.to_sym][:type] if field_defined(field)
177
199
  return nil
178
200
  end
201
+
202
+ private
203
+ def field_defined(field)
204
+ !!kitsune_admin[:fields][field.to_sym]
205
+ end
179
206
 
180
207
  def field_options(field)
181
208
  options = kitsune_admin[:fields][field.to_sym][:options] if field_defined(field)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kitsune
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Mongeau <matt@toastyapps.com>
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-10 00:00:00 -05:00
12
+ date: 2009-11-24 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -34,6 +34,7 @@ files:
34
34
  - Rakefile
35
35
  - README.textile
36
36
  - app/controllers/admin/kitsune/application_controller.rb
37
+ - app/controllers/admin/kitsune/kitsune_controller.rb
37
38
  - app/controllers/admin/kitsune/models_controller.rb
38
39
  - app/controllers/admin/kitsune/pages_controller.rb
39
40
  - app/controllers/admin/kitsune/records_controller.rb
@@ -46,6 +47,7 @@ files:
46
47
  - app/views/admin/kitsune/pages/index.html.erb
47
48
  - app/views/admin/kitsune/pages/new.html.erb
48
49
  - app/views/admin/kitsune/records/_form.html.erb
50
+ - app/views/admin/kitsune/records/_list.html.erb
49
51
  - app/views/admin/kitsune/records/edit.html.erb
50
52
  - app/views/admin/kitsune/records/index.html.erb
51
53
  - app/views/admin/kitsune/records/new.html.erb