super_crud 0.1.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.
- data/LICENSE +20 -0
- data/README.rdoc +18 -0
- data/Rakefile +59 -0
- data/lib/super_crud.rb +3 -0
- data/lib/super_crud/controller.rb +166 -0
- data/lib/super_crud/helper.rb +116 -0
- data/lib/super_crud/model.rb +0 -0
- data/test/helper.rb +10 -0
- data/test/test_super_crud.rb +7 -0
- metadata +64 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 John Paul Narowski
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
= super_crud
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but
|
13
|
+
bump version in a commit by itself I can ignore when I pull)
|
14
|
+
* Send me a pull request. Bonus points for topic branches.
|
15
|
+
|
16
|
+
== Copyright
|
17
|
+
|
18
|
+
Copyright (c) 2010 John Paul Narowski. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "super_crud"
|
8
|
+
gem.summary = %Q{Abstracts some helper model and controller logic}
|
9
|
+
gem.description = %Q{This is a admin system abstraction to get you up and running faster}
|
10
|
+
gem.email = "jnarowski@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/jnarowski/super_crud"
|
12
|
+
gem.authors = ["John Paul Narowski"]
|
13
|
+
gem.files = [
|
14
|
+
"LICENSE",
|
15
|
+
"README.rdoc",
|
16
|
+
"Rakefile",
|
17
|
+
Dir["{spec,lib}/**/*"],
|
18
|
+
"test/helper.rb",
|
19
|
+
"test/test_metastrano.rb"
|
20
|
+
]
|
21
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
22
|
+
end
|
23
|
+
rescue LoadError
|
24
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
25
|
+
end
|
26
|
+
|
27
|
+
require 'rake/testtask'
|
28
|
+
Rake::TestTask.new(:test) do |test|
|
29
|
+
test.libs << 'lib' << 'test'
|
30
|
+
test.pattern = 'test/**/test_*.rb'
|
31
|
+
test.verbose = true
|
32
|
+
end
|
33
|
+
|
34
|
+
begin
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
end
|
41
|
+
rescue LoadError
|
42
|
+
task :rcov do
|
43
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
task :test => :check_dependencies
|
48
|
+
|
49
|
+
task :default => :test
|
50
|
+
|
51
|
+
require 'rake/rdoctask'
|
52
|
+
Rake::RDocTask.new do |rdoc|
|
53
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
54
|
+
|
55
|
+
rdoc.rdoc_dir = 'rdoc'
|
56
|
+
rdoc.title = "super_crud #{version}"
|
57
|
+
rdoc.rdoc_files.include('README*')
|
58
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
59
|
+
end
|
data/lib/super_crud.rb
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
module SuperCrud
|
2
|
+
module Controller
|
3
|
+
|
4
|
+
# def self.included(base)
|
5
|
+
# base.extend(ClassMethods)
|
6
|
+
# end
|
7
|
+
#
|
8
|
+
# module ClassMethods
|
9
|
+
# def acts_as_super_crud
|
10
|
+
# include SuperCrud::Controller
|
11
|
+
# end
|
12
|
+
# end
|
13
|
+
|
14
|
+
#----------------------------------------------------------------
|
15
|
+
# controller functionality
|
16
|
+
# -- you can override or extend if you want
|
17
|
+
#----------------------------------------------------------------
|
18
|
+
|
19
|
+
def display_list
|
20
|
+
load_params
|
21
|
+
load_objects
|
22
|
+
update_list
|
23
|
+
end
|
24
|
+
|
25
|
+
def index
|
26
|
+
load_params
|
27
|
+
load_objects
|
28
|
+
end
|
29
|
+
|
30
|
+
def edit
|
31
|
+
respond_to do |format|
|
32
|
+
format.html # edit.html
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def show
|
37
|
+
load_object
|
38
|
+
end
|
39
|
+
|
40
|
+
#----------------------------------------------------------------
|
41
|
+
# CRUD helper methods
|
42
|
+
#----------------------------------------------------------------
|
43
|
+
|
44
|
+
def _create(object, path = nil)
|
45
|
+
respond_to do |format|
|
46
|
+
if object.save
|
47
|
+
load_objects
|
48
|
+
flash[:notice] = "#{@_name} was successfully created."
|
49
|
+
format.html { redirect_to(set_path(object, path)) }
|
50
|
+
else
|
51
|
+
format.html { render :action => "new" }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def _update(object, path = nil)
|
57
|
+
respond_to do |format|
|
58
|
+
if object.update_attributes(@_attributes)
|
59
|
+
flash[:notice] = "#{@_name} was successfully updated."
|
60
|
+
format.html { redirect_to(set_path(object, path)) }
|
61
|
+
else
|
62
|
+
format.html { render :action => "edit" }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def _destroy(name, object, object_name)
|
68
|
+
object.destroy
|
69
|
+
respond_to do |format|
|
70
|
+
format.js { simple_destroy(name, object, :message => "#{object_name} has been removed.") }
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
#----------------------------------------------------------------
|
75
|
+
# helper functions
|
76
|
+
#----------------------------------------------------------------
|
77
|
+
|
78
|
+
# used to set the path in the #_update and #_create methods
|
79
|
+
def set_path(object, path)
|
80
|
+
path.blank? ? "#{@_path}/#{object.id}" : path
|
81
|
+
end
|
82
|
+
|
83
|
+
# if the user has searched, add the search filter parameter
|
84
|
+
# this will pop the box in the top of the table listing
|
85
|
+
def add_search_if_present(collections)
|
86
|
+
if params[:search]
|
87
|
+
@filter_text = "Search for '#{params[:search]}'"
|
88
|
+
collections.search(params[:search])
|
89
|
+
else
|
90
|
+
collections
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# This is used to add extra parameters to the column_heading and pagination filtering
|
95
|
+
# in the controller if you wanted to add the params[:search] to the filters
|
96
|
+
# EG: add_extra_param(:search)
|
97
|
+
#
|
98
|
+
def add_filter_param(name)
|
99
|
+
@extra_params[name] = params[name] unless params[name].blank?
|
100
|
+
end
|
101
|
+
|
102
|
+
#----------------------------------------------------------------
|
103
|
+
# searching and sorting helpers
|
104
|
+
#----------------------------------------------------------------
|
105
|
+
|
106
|
+
def get_order(ordering, exact = false)
|
107
|
+
if ordering
|
108
|
+
order_array = ordering.split("_")
|
109
|
+
direction = (order_array[0] == 'ascend') ? 'ASC' : 'DESC'
|
110
|
+
column = ''
|
111
|
+
order_array.each_with_index {|a, index| column += "#{a}_" if index > 1}
|
112
|
+
column = column[0..-2]
|
113
|
+
"#{column} #{direction}"
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
#----------------------------------------------------------------
|
118
|
+
# shared rjs methods
|
119
|
+
#----------------------------------------------------------------
|
120
|
+
|
121
|
+
def update_list(options = {})
|
122
|
+
options[:sidebar] = 'side_menu'
|
123
|
+
render :update do |page|
|
124
|
+
page.replace_html 'list', :partial => 'list'
|
125
|
+
page.replace_html 'side-menu', :partial => options[:sidebar] if options[:reload_sidebar]
|
126
|
+
page.replace_html options[:reload_partial], :partial => options[:reload_partial] if options[:reload_partial]
|
127
|
+
# show the advanced filter text
|
128
|
+
unless @filter_text.blank?
|
129
|
+
page.replace_html 'filter', filter_results
|
130
|
+
page.show "filter"
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def simple_destroy(id, object, options = {})
|
136
|
+
dom_id = options[:id] ? options[:id] : "#{id}-#{object.id}"
|
137
|
+
render :update do |page|
|
138
|
+
page.remove dom_id
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
#----------------------------------------------------------------
|
143
|
+
# private functionaltiy
|
144
|
+
#----------------------------------------------------------------
|
145
|
+
|
146
|
+
private
|
147
|
+
|
148
|
+
# this loads params for the index page
|
149
|
+
# sets defaults for column sorting, searching etc
|
150
|
+
def load_params
|
151
|
+
super
|
152
|
+
@extra_params = {}
|
153
|
+
@ordering ||= 'descend_by_created_at'
|
154
|
+
add_filter_param(:search)
|
155
|
+
end
|
156
|
+
|
157
|
+
# sets page default options, selected tab states ETC
|
158
|
+
def initialize_page
|
159
|
+
super
|
160
|
+
action = action_name == 'index' ? "#{@_name}s" : "#{action_name.capitalize} #{@_name}"
|
161
|
+
@page_title = action
|
162
|
+
@render_options[:selected_tab] = @_selected_tab
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
module SuperCrud
|
2
|
+
module Helper
|
3
|
+
#----------------------------------------------------------------
|
4
|
+
# framework stuff
|
5
|
+
#----------------------------------------------------------------
|
6
|
+
|
7
|
+
def filter_results
|
8
|
+
out = ""
|
9
|
+
out += "<div id=\"filter-results-count\" class=\"float-right\">#{@filter_count} found #{link_to(icon('cross'), {:controller => controller_name})}</div>" if @filter_count
|
10
|
+
out += "<div id=\"filter-results\">#{@filter_text}</div>#{}" if @filter_text
|
11
|
+
out
|
12
|
+
end
|
13
|
+
|
14
|
+
def super_header(name, &block)
|
15
|
+
heading = "<h1>#{name}</h1>"
|
16
|
+
concat("<div id='sub-header'>#{capture(&block)}#{heading}#{float_clear}</div>")
|
17
|
+
end
|
18
|
+
|
19
|
+
def super_table(&block)
|
20
|
+
table_start = '<div id="filter" class="rounded" style="display: none;"></div>
|
21
|
+
<table class="stripped-list list" cellspacing="0" width="750px">'
|
22
|
+
concat("#{table_start}#{capture(&block)}</table>")
|
23
|
+
end
|
24
|
+
|
25
|
+
def super_row(name, object, iterator = nil, &block)
|
26
|
+
line = (iterator ? iterator % 2 : 0)
|
27
|
+
concat("<tr id='#{name}-#{object.id}' class='line-#{line} padded-td'>#{capture(&block)}</tr>")
|
28
|
+
end
|
29
|
+
|
30
|
+
def super_paginate(collection)
|
31
|
+
"<div id='table-footer'>#{super_custom_remote_paginate(collection)}</div>"
|
32
|
+
end
|
33
|
+
|
34
|
+
# table header
|
35
|
+
def table_header(width, name, column, options = {})
|
36
|
+
filler = options[:plain_text].blank? ? super_column_heading(name, column) : name
|
37
|
+
"<th width='#{width}' class='#{options[:class]}'>#{filler}</th>"
|
38
|
+
end
|
39
|
+
|
40
|
+
def super_button(name, link, options = {})
|
41
|
+
div_style = options.delete(:div_style)
|
42
|
+
"<div class='button' style='#{div_style}'>#{link_to(name, link, options)}</div>"
|
43
|
+
end
|
44
|
+
|
45
|
+
def super_menu_item(name, link, selected = nil, options = {})
|
46
|
+
options[:class] = 'selected' if selected == @render_options[:selected_tab] && !@render_options[:selected_tab].blank?
|
47
|
+
"<li>#{link_to(name, link, options)}</li>"
|
48
|
+
end
|
49
|
+
|
50
|
+
#----------------------------------------------------------------
|
51
|
+
# general helpers
|
52
|
+
#----------------------------------------------------------------
|
53
|
+
|
54
|
+
def set_admin_url(object, path)
|
55
|
+
object.new_record? ? "/admin/#{path}/" : "/admin/#{path}/#{object.id}"
|
56
|
+
end
|
57
|
+
|
58
|
+
def super_icon(type, options = {})
|
59
|
+
width = 16
|
60
|
+
alt = nil
|
61
|
+
file_name = "#{type}.png"
|
62
|
+
"<img src=\"/images/icons/#{file_name}\" alt=\"#{alt ? alt : type}\" #{"title=\"#{alt}\"" if !alt.blank?} style=\"width: #{width}px;\" />"
|
63
|
+
end
|
64
|
+
|
65
|
+
#----------------------------------------------------------------
|
66
|
+
# show helpers
|
67
|
+
#----------------------------------------------------------------
|
68
|
+
|
69
|
+
def super_show_item(name, value)
|
70
|
+
value = value.blank? ? '<i>null</i>' : value
|
71
|
+
"<p>
|
72
|
+
<label class='strong'>#{name}</label>
|
73
|
+
#{value}
|
74
|
+
</p>"
|
75
|
+
end
|
76
|
+
|
77
|
+
#----------------------------------------------------------------
|
78
|
+
# table sorting and filtering helpers
|
79
|
+
#----------------------------------------------------------------
|
80
|
+
|
81
|
+
def super_column_heading(display_name, attribute, controller = controller_name)
|
82
|
+
options = {
|
83
|
+
:controller => controller,
|
84
|
+
:action => 'display_list',
|
85
|
+
:ordering => ordering(attribute),
|
86
|
+
:attribute => attribute.to_s
|
87
|
+
}.merge!(@extra_params)
|
88
|
+
link_to("#{display_name} #{super_triangle(attribute)}", options, :class => 'column-header')
|
89
|
+
end
|
90
|
+
|
91
|
+
def super_custom_remote_paginate(collection, options = {})
|
92
|
+
options[:ordering] ||= @ordering
|
93
|
+
options[:attribute] ||= @attribute
|
94
|
+
options[:controller] ||= controller_name
|
95
|
+
options[:action] ||= 'display_list'
|
96
|
+
options[:method] ||= 'post'
|
97
|
+
options.merge!(@extra_params)
|
98
|
+
"<div class='pagination'>#{will_paginate(collection, :params => options, :remote => {})}</div>#{float_clear}"
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
def super_triangle(attribute)
|
103
|
+
return "▲" if @ordering == "ascend_by_#{attribute}"
|
104
|
+
return "▼" if @ordering == "descend_by_#{attribute}"
|
105
|
+
end
|
106
|
+
|
107
|
+
def ordering(attribute)
|
108
|
+
if @ordering == "ascend_by_#{attribute}" then
|
109
|
+
"descend_by_#{attribute}"
|
110
|
+
else
|
111
|
+
"ascend_by_#{attribute}"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
File without changes
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: super_crud
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Paul Narowski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-25 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: This is a admin system abstraction to get you up and running faster
|
17
|
+
email: jnarowski@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- LICENSE
|
27
|
+
- README.rdoc
|
28
|
+
- Rakefile
|
29
|
+
- lib/super_crud.rb
|
30
|
+
- lib/super_crud/controller.rb
|
31
|
+
- lib/super_crud/helper.rb
|
32
|
+
- lib/super_crud/model.rb
|
33
|
+
- test/helper.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://github.com/jnarowski/super_crud
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options:
|
40
|
+
- --charset=UTF-8
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.3.5
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: Abstracts some helper model and controller logic
|
62
|
+
test_files:
|
63
|
+
- test/helper.rb
|
64
|
+
- test/test_super_crud.rb
|