tabletastic 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -19,3 +19,4 @@ rdoc
19
19
  pkg
20
20
 
21
21
  ## PROJECT::SPECIFIC
22
+ doc/
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,20 @@
1
+ v0.1.3 (Dec 28, 2009)
2
+ * Changing destroy action to have confirmation by default
3
+
4
+ v0.1.2 (Nov 25, 2009)
5
+
6
+ * Added ability to namespace :actions links (i.e. /admin/posts/1)
7
+
8
+ v0.1.1 (Nov 22, 2009)
9
+
10
+ * Added :actions option to data method for common show, edit, destroy links
11
+
12
+ v0.1.0 (Nov 17, 2009)
13
+
14
+ * First useful release
15
+ * Added documentation
16
+ * "Lazy" attributes can be supplied as blocks to cell method
17
+
18
+ v0.0.1 (Nov 15, 2009)
19
+
20
+ * Beta release, proof-of-concept
data/README.rdoc CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Inspired by the projects table_builder and formtastic,
4
4
  I realized how often I created tables for my active record collections.
5
- This is my attempt to simply this (the default scaffold):
5
+ This is my attempt to simplify this (the default scaffold):
6
6
 
7
7
  <table>
8
8
  <tr>
@@ -25,17 +25,13 @@ This is my attempt to simply this (the default scaffold):
25
25
  into this:
26
26
 
27
27
  <% table_for(@posts) do |t| %>
28
- <%= t.data :title, :body, :author %>
28
+ <%= t.data :actions => :all %>
29
29
  <% end %>
30
30
 
31
31
  and still output the same effective results, but with all the semantic
32
- goodness that tabular data should have, i.e. a +<thead>+ and +<tbody>+ element.
32
+ goodness that tabular data should have, i.e. a +thead+ and +tbody+ element.
33
33
 
34
34
 
35
- == Warning
36
-
37
- This project is still being actively developed. As such, future updates might not be backwards-compatible.
38
-
39
35
  == Installation
40
36
 
41
37
  In your Rails project, as a gem:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
data/lib/tabletastic.rb CHANGED
@@ -27,11 +27,15 @@ module Tabletastic
27
27
 
28
28
  class TableBuilder
29
29
  @@association_methods = %w[display_name full_name name title username login value to_s]
30
+ @@default_hidden_columns = %w[created_at updated_at created_on updated_on lock_version version]
31
+ @@destroy_confirm_message = "Are you sure?"
32
+
30
33
  attr_accessor :field_labels
31
- attr_reader :collection, :klass
34
+ attr_reader :collection, :klass, :fields
32
35
 
33
36
  def initialize(collection, klass, template)
34
37
  @collection, @klass, @template = collection, klass, template
38
+ @field_labels = []
35
39
  end
36
40
 
37
41
  # builds up the fields that the table will include,
@@ -48,12 +52,13 @@ module Tabletastic
48
52
  def data(*args, &block) # :yields: tablebody
49
53
  options = args.extract_options!
50
54
  if block_given?
55
+ @fields = []
51
56
  yield self
52
57
  action_cells(options[:actions], options[:action_prefix])
53
58
  @template.concat(head)
54
59
  @template.concat(body)
55
60
  else
56
- @fields = args.empty? ? fields : args
61
+ @fields = args.empty? ? active_record_fields : args
57
62
  @field_labels = fields.map { |f| f.to_s.humanize }
58
63
  action_cells(options[:actions], options[:action_prefix])
59
64
  [head, body].join("")
@@ -81,61 +86,36 @@ module Tabletastic
81
86
  #
82
87
  # <td>$1.50</td>
83
88
  #
84
- def cell(*args, &block)
89
+ def cell(*args, &proc)
85
90
  options = args.extract_options!
86
- @field_labels ||= []
87
- @fields ||= []
88
-
89
- method_or_attribute = args.first.to_sym
91
+ method = args.first.to_sym
92
+ method_or_proc = block_given? ? proc : method
90
93
 
91
- method_or_attribute_or_proc = if block_given?
92
- block.to_proc
93
- else
94
- method_or_attribute
95
- end
96
-
97
- if cell_html = options.delete(:cell_html)
98
- @fields << [method_or_attribute_or_proc, cell_html]
99
- else
100
- @fields << method_or_attribute_or_proc
101
- end
94
+ @fields << [method_or_proc, options.delete(:cell_html)]
102
95
 
103
- if heading = options.delete(:heading)
104
- @field_labels << heading
105
- else
106
- @field_labels << method_or_attribute.to_s.humanize
107
- end
96
+ @field_labels << (options.delete(:heading) || method.to_s.humanize)
108
97
  # Since this will likely be called with <%= %> (aka 'concat'), explicitly return an empty string
109
98
  # This suppresses unwanted output
110
99
  return ""
111
100
  end
112
101
 
113
102
  def head
114
- @field_labels ||= fields
115
103
  content_tag(:thead) do
116
- header_row
117
- end
118
- end
119
-
120
- def header_row
121
- content_tag(:tr) do
122
- @field_labels.inject("") do |result,field|
123
- result += content_tag(:th, field)
104
+ content_tag(:tr) do
105
+ self.field_labels.inject("") do |result,field|
106
+ result += content_tag(:th, field)
107
+ end
124
108
  end
125
109
  end
126
110
  end
127
111
 
128
112
  def body
129
113
  content_tag(:tbody) do
130
- body_rows
131
- end
132
- end
133
-
134
- def body_rows
135
- @collection.inject("") do |rows, record|
136
- rowclass = @template.cycle("odd","even")
137
- rows += @template.content_tag_for(:tr, record, :class => rowclass) do
138
- cells_for_row(record)
114
+ @collection.inject("") do |rows, record|
115
+ rowclass = @template.cycle("odd","even")
116
+ rows += @template.content_tag_for(:tr, record, :class => rowclass) do
117
+ cells_for_row(record)
118
+ end
139
119
  end
140
120
  end
141
121
  end
@@ -181,19 +161,14 @@ module Tabletastic
181
161
  case action
182
162
  when :show
183
163
  @template.link_to("Show", compound_resource)
184
- when :edit
185
- @template.link_to("Edit", @template.polymorphic_path(compound_resource, :action => :edit))
186
164
  when :destroy
187
- @template.link_to("Destroy", compound_resource, :method => :delete)
165
+ @template.link_to("Destroy", compound_resource, :method => :delete, :confirm => @@destroy_confirm_message)
166
+ else # edit, other resource GET actions
167
+ @template.link_to(action.to_s.titleize, @template.polymorphic_path(compound_resource, :action => action))
188
168
  end
189
169
  end
190
170
  end
191
171
 
192
- def fields
193
- return @fields if defined?(@fields)
194
- @fields = @collection.empty? ? [] : active_record_fields
195
- end
196
-
197
172
  protected
198
173
 
199
174
  def detect_string_method(association)
@@ -203,27 +178,23 @@ module Tabletastic
203
178
 
204
179
  def active_record_fields
205
180
  return [] if klass.blank?
206
- # normal content columns
207
181
  fields = klass.content_columns.map(&:name)
208
-
209
- # active record associations
210
- if klass.respond_to?(:reflect_on_all_associations)
211
- associations = klass.reflect_on_all_associations(:belongs_to)
212
- associations = associations.map(&:name)
213
- fields += associations
214
- end
215
-
216
- # remove utility columns by default
217
- fields -= %w[created_at updated_at created_on updated_on lock_version version]
182
+ fields += active_record_association_reflections
183
+ fields -= @@default_hidden_columns
218
184
  fields = fields.map(&:to_sym)
219
185
  end
220
186
 
221
- def content_tag(name, content = nil, options = nil, escape = true, &block)
222
- @template.content_tag(name, content, options, escape, &block)
187
+ def active_record_association_reflections
188
+ return [] unless klass.respond_to?(:reflect_on_all_associations)
189
+ associations = klass.reflect_on_all_associations(:belongs_to).map(&:name)
223
190
  end
224
191
 
225
192
  private
226
193
 
194
+ def content_tag(name, content = nil, options = nil, escape = true, &block)
195
+ @template.content_tag(name, content, options, escape, &block)
196
+ end
197
+
227
198
  def send_or_call(object, duck)
228
199
  if duck.respond_to?(:call)
229
200
  duck.call(object)
@@ -158,7 +158,7 @@ describe "Tabletastic#table_for" do
158
158
  table_for(@posts) do |t|
159
159
  concat(t.data(:actions => :destroy))
160
160
  end
161
- output_buffer.should have_table_with_tag("a[@href=/posts/#{@post.id}]", "Destroy")
161
+ output_buffer.should have_table_with_tag("a[@href=/posts/#{@post.id}]")
162
162
  output_buffer.should have_table_with_tag("th", "")
163
163
  end
164
164
 
@@ -339,20 +339,3 @@ describe "Tabletastic#table_for" do
339
339
  end
340
340
  end
341
341
  end
342
-
343
- describe TableBuilder do
344
- before do
345
- mock_everything
346
- ::Post.stub!(:content_columns).and_return([mock('column', :name => 'title'), mock('column', :name => 'body'), mock('column', :name => 'created_at')])
347
- @posts = [@post, Post.new]
348
- @builder = TableBuilder.new(@posts, ::Post, nil)
349
- end
350
-
351
- it "should detect attributes" do
352
- @builder.fields.should include(:title)
353
- end
354
-
355
- it "should reject marked attributes" do
356
- @builder.fields.should_not include(:created_at)
357
- end
358
- end
data/tabletastic.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{tabletastic}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Joshua Davey"]
12
- s.date = %q{2009-11-25}
12
+ s.date = %q{2009-12-28}
13
13
  s.description = %q{A table builder for active record collections that produces semantically rich and accessible markup}
14
14
  s.email = %q{josh@joshuadavey.com}
15
15
  s.extra_rdoc_files = [
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.files = [
20
20
  ".document",
21
21
  ".gitignore",
22
+ "CHANGELOG.rdoc",
22
23
  "LICENSE",
23
24
  "README.rdoc",
24
25
  "Rakefile",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tabletastic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Davey
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-25 00:00:00 -06:00
12
+ date: 2009-12-28 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -34,6 +34,7 @@ extra_rdoc_files:
34
34
  files:
35
35
  - .document
36
36
  - .gitignore
37
+ - CHANGELOG.rdoc
37
38
  - LICENSE
38
39
  - README.rdoc
39
40
  - Rakefile