inherited_rails_views 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ .idea/
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use --create 1.9.2@irv
data/Readme.md ADDED
@@ -0,0 +1,21 @@
1
+ # DRY views
2
+
3
+ The [old inherited views project](https://github.com/fredwu/inherited_resources_views) by Fred was a great idea.
4
+ With Rails 3.1, inherited views got even simpler.
5
+ This Project provides you with basic views for your CRUD actions. No generators, no nothing ;)
6
+
7
+ # Install
8
+
9
+ Add the inherited_rails_views gem to your Applications Gemfile:
10
+
11
+ gem 'inherited_rails_views'
12
+
13
+ # Usage
14
+
15
+ class TestsController < InheritedResources::Base
16
+ include InheritedRailsViews
17
+
18
+ # now all attributes of collection are shown in the views.
19
+ # You can, however, specify which ones should be used.
20
+ inherited_attributes :attr1, :attr2, :attr3 => {:value => "Custom Titled Attribute", :type => :checkbox}
21
+ end
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.email = ["robin@wenglewski.de"]
10
10
  s.homepage = "https://github.com/rweng/inherited_rails_views"
11
11
  s.summary = %q{inherited views for Rails > 3.1}
12
- # s.description = %q{TODO: Write a gem description}
12
+ # s.description = %q{}
13
13
 
14
14
  # s.rubyforge_project = "inherited-views"
15
15
 
@@ -19,7 +19,9 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  # specify any dependencies here; for example:
22
- # s.add_development_dependency "rspec"
22
+ s.add_development_dependency "rspec"
23
+ s.add_development_dependency "activesupport"
24
+ s.add_development_dependency "actionpack"
23
25
  # s.add_runtime_dependency "rest-client"
24
- s.add_dependency "haml-rails"
26
+ s.add_dependency "dsl_accessors"
25
27
  end
@@ -1,12 +1,70 @@
1
1
  require "inherited_rails_views/version"
2
- require "inherited_rails_views/helpers/application_helper"
2
+
3
+ require 'dsl_accessors'
4
+
5
+ require 'active_support/core_ext/class'
6
+ require 'active_support/core_ext/array'
7
+ require 'active_support/core_ext/module'
8
+
9
+ require 'active_support/concern'
3
10
 
4
11
  module InheritedRailsViews
5
- if defined?(::Rails) and Rails.version >="3.1"
6
- class Engine < ::Rails::Engine
7
- end
8
-
9
- ActionController::Base.append_view_path( File.expand_path "../inherited_rails_views/views", __FILE__ )
10
- ActionController::Base.extend ApplicationHelper
11
- end
12
- end
12
+ extend ActiveSupport::Concern
13
+
14
+ included do
15
+ append_view_path(File.expand_path "../inherited_rails_views/views", __FILE__)
16
+ helper_method :inherited_attributes
17
+ end
18
+
19
+ module InstanceMethods
20
+ include DslAccessors
21
+ attr_accessor :inherited_attributes
22
+
23
+ # sets the attributes to be displayed by inherited_rails_views
24
+ #
25
+ # inherited_attributes :method1, :method2, :method3
26
+ #
27
+ # It is also possible to provide a hash
28
+ # inherited_attributes :method1 => true, :method2 => "Title of Method 2"
29
+ #
30
+ # It is even possible to set the form_helper method to be used
31
+ #
32
+ # inherited_attributes :method1 => {:value => true, :type => :text_field},
33
+ # :method2 => {:value => "Password", :type => :password_field}
34
+ #
35
+ # They array and hash can also be combined
36
+ #
37
+ # inherited_attributes :method1, :method2, :method3 => {:value => "Password", :type => :password_field}
38
+ #
39
+ def set_inherited_attributes(*args)
40
+ # extract the hash attributes
41
+ options = args.extract_options!
42
+
43
+ # reject all methods that have no value key
44
+ options.select! { |k, v| v[:value].is_a?(String) }
45
+
46
+ options.each_pair do |k, v|
47
+ v[:type] ||= :text_field
48
+ end
49
+
50
+ # add array methods to the hash and store the result in @inherited_attributes
51
+ @inherited_attributes = args.inject(options) do |h, e|
52
+ h[e] = {:value => e.to_s.titleize, :type => :text_field}
53
+ h
54
+ end
55
+ end
56
+
57
+ def inherited_attributes_with_defaults(*args)
58
+ inherited_attributes_without_defaults(*args) || default_inherited_attributes
59
+ end
60
+ alias_method_chain :inherited_attributes, :defaults
61
+
62
+ private
63
+ def default_inherited_attributes
64
+ klass = collection.respond_to?(:klass) ? collection.klass : collection.class
65
+ attributes = klass.attribute_names - %w(id created_at updated_at)
66
+ self.inherited_attributes *attributes.map(&:to_sym)
67
+ end
68
+ end
69
+
70
+ end
@@ -1,3 +1,3 @@
1
1
  module InheritedRailsViews
2
- VERSION = "0.0.2"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -0,0 +1,27 @@
1
+ <%= form_for resource do |f| %>
2
+ <% if resource.errors.any? %>
3
+ <div id="error_explanation">
4
+ <h2><%= "#{pluralize(resource.errors.count, "error")} prohibited this news from being saved:" %></h2>
5
+ <ul>
6
+ <% resource.errors.full_messages.each do |msg| %>
7
+ <li><%= msg %></li>
8
+ <% end %>
9
+ </ul>
10
+ </div>
11
+ <% end %>
12
+
13
+ <table>
14
+ <% inherited_attributes.each_pair do |k, v| %>
15
+ <tr>
16
+ <td class="field"><%= f.label v[:value] %></td>
17
+ <td class="field"><%= f.send(v[:type], k) %></td>
18
+ </tr>
19
+ <% end %>
20
+ <tr>
21
+ <td>
22
+ <td class="actions">
23
+ <%= f.submit "Save" %>
24
+ </td>
25
+ </tr>
26
+ </table>
27
+ <% end %>
@@ -0,0 +1,7 @@
1
+ <h1>Editing <%= resource.class.to_s.titleize %></h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Show', resource %>
6
+ |
7
+ <%= link_to 'Back', collection_path %>
@@ -0,0 +1,30 @@
1
+ <h1>
2
+ Listing
3
+ <%= collection.name %>
4
+ </h1>
5
+
6
+ <table>
7
+ <tr>
8
+ <% inherited_attributes.each_pair do |k, v| %>
9
+ <th><%= v[:value] %></th>
10
+ <% end %>
11
+ <th></th>
12
+ <th></th>
13
+ <th></th>
14
+ </tr>
15
+
16
+ <% collection.each do |res| %>
17
+ <tr>
18
+ <% inherited_attributes.each_pair do |k, v| %>
19
+ <td><%= res.send(k) %></td>
20
+ <% end %>
21
+ <td><%= link_to 'Show', res %> </td>
22
+ <td><%= link_to 'Edit', edit_resource_path(res) %> </td>
23
+ <td><%= link_to 'Destroy', res, :confirm => 'Are you sure?', :method => :delete %> </td>
24
+ </tr>
25
+ <% end %>
26
+ </table>
27
+
28
+ <br/>
29
+ <%= link_to "Create #{collection.name}", new_resource_path %>
30
+
@@ -0,0 +1,9 @@
1
+ <h1>
2
+ New
3
+ <%= resource.class.to_s.titleize %>
4
+ </h1>
5
+
6
+ <%= render 'form' %>
7
+
8
+ <%= link_to 'Back', collection_path %>
9
+
@@ -0,0 +1,16 @@
1
+ <p id="notice"><%= notice %></p>
2
+
3
+ <table>
4
+ <% inherited_attributes.each_pair do |k,v| %>
5
+ <tr>
6
+ <td>
7
+ <b>
8
+ <%= v[:value] %></b>
9
+ <td>
10
+ <%= resource.send k %></td>
11
+ </tr>
12
+ <% end %>
13
+ </table>
14
+ <%= link_to 'Edit', edit_resource_path(resource) %>
15
+ |
16
+ <%= link_to 'Back', collection_path %>
@@ -0,0 +1,38 @@
1
+ require 'active_support/core_ext/string'
2
+
3
+ require 'inherited_rails_views'
4
+ require 'action_controller'
5
+
6
+ class TestClass < ActionController::Base
7
+ include InheritedRailsViews
8
+ end
9
+
10
+
11
+ describe "InheritedRailsViews" do
12
+
13
+ before do
14
+ @klass = TestClass.new
15
+ end
16
+
17
+ it "should provide the inherited_attributes method" do
18
+ @klass.should respond_to(:inherited_attributes)
19
+ end
20
+
21
+ it "should transform a list of methods to a hash" do
22
+ @klass.inherited_attributes :a, :hello_world
23
+ @klass.inherited_attributes.should eql({:a => {:value => 'A', :type => :text_field},
24
+ :hello_world => {:value => "Hello World", :type => :text_field}})
25
+ end
26
+
27
+ it "should accept a hash" do
28
+ @klass.inherited_attributes :hello_world, :foo_bar => {:value => "FBar"},
29
+ :bar_foo => {:value => "Bar Foo",
30
+ :type => :checkbox}
31
+
32
+ @klass.instance_variable_get(:@inherited_attributes).should eql({
33
+ :hello_world => {:value => "Hello World", :type => :text_field},
34
+ :foo_bar => {:value => "FBar", :type => :text_field},
35
+ :bar_foo => {:value => "Bar Foo", :type => :checkbox}
36
+ })
37
+ end
38
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inherited_rails_views
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,44 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-30 00:00:00.000000000Z
12
+ date: 2011-09-11 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: haml-rails
16
- requirement: &70339569922680 !ruby/object:Gem::Requirement
15
+ name: rspec
16
+ requirement: &70105623241200 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70105623241200
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ requirement: &70105623240600 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70105623240600
36
+ - !ruby/object:Gem::Dependency
37
+ name: actionpack
38
+ requirement: &70105623240020 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70105623240020
47
+ - !ruby/object:Gem::Dependency
48
+ name: dsl_accessors
49
+ requirement: &70105623239360 !ruby/object:Gem::Requirement
17
50
  none: false
18
51
  requirements:
19
52
  - - ! '>='
@@ -21,7 +54,7 @@ dependencies:
21
54
  version: '0'
22
55
  type: :runtime
23
56
  prerelease: false
24
- version_requirements: *70339569922680
57
+ version_requirements: *70105623239360
25
58
  description:
26
59
  email:
27
60
  - robin@wenglewski.de
@@ -30,18 +63,19 @@ extensions: []
30
63
  extra_rdoc_files: []
31
64
  files:
32
65
  - .gitignore
66
+ - .rvmrc
33
67
  - Gemfile
34
68
  - Rakefile
35
- - Readme.textile
69
+ - Readme.md
36
70
  - inherited_rails_views.gemspec
37
71
  - lib/inherited_rails_views.rb
38
- - lib/inherited_rails_views/helpers/application_helper.rb
39
72
  - lib/inherited_rails_views/version.rb
40
- - lib/inherited_rails_views/views/application/_form.html.haml
41
- - lib/inherited_rails_views/views/application/edit.html.haml
42
- - lib/inherited_rails_views/views/application/index.html.haml
43
- - lib/inherited_rails_views/views/application/new.html.haml
44
- - lib/inherited_rails_views/views/application/show.html.haml
73
+ - lib/inherited_rails_views/views/application/_form.html.erb
74
+ - lib/inherited_rails_views/views/application/edit.html.erb
75
+ - lib/inherited_rails_views/views/application/index.html.erb
76
+ - lib/inherited_rails_views/views/application/new.html.erb
77
+ - lib/inherited_rails_views/views/application/show.html.erb
78
+ - spec/inherited_rails_views_spec.rb
45
79
  homepage: https://github.com/rweng/inherited_rails_views
46
80
  licenses: []
47
81
  post_install_message:
@@ -66,4 +100,5 @@ rubygems_version: 1.8.10
66
100
  signing_key:
67
101
  specification_version: 3
68
102
  summary: inherited views for Rails > 3.1
69
- test_files: []
103
+ test_files:
104
+ - spec/inherited_rails_views_spec.rb
data/Readme.textile DELETED
@@ -1,11 +0,0 @@
1
- h1. DRY views
2
-
3
- The "old inherited views project":https://github.com/fredwu/inherited_resources_views by Fred was a great idea. With Rails 3.1, inherited views got even simpler. This Project provides you with basic views for your CRUD actions. No generators, no nothing ;)
4
-
5
- h1. Install
6
-
7
- Add the inherited_rails_views-rails gem to your Applications Gemfile:
8
-
9
- bc. gem 'inherited_rails_views'
10
-
11
- Now you can delete your scaffolded views folder.
@@ -1,6 +0,0 @@
1
- module ApplicationHelper
2
- def shown_attrs
3
- klass = collection.respond_to?(:klass) ? collection.klass : collection.class
4
- klass.attribute_names - %w(id created_at updated_at)
5
- end
6
- end
@@ -1,19 +0,0 @@
1
- = form_for resource do |f|
2
- -if resource.errors.any?
3
- #error_explanation
4
- %h2= "#{pluralize(resource.errors.count, "error")} prohibited this news from being saved:"
5
- %ul
6
- - resource.errors.full_messages.each do |msg|
7
- %li= msg
8
-
9
- %table
10
- - shown_attrs.each do |attr|
11
- %tr
12
- %td.field
13
- = f.label attr.titleize
14
- %td.field
15
- = f.text_field attr.to_sym
16
- %tr
17
- %td
18
- %td.actions
19
- = f.submit 'Save'
@@ -1,10 +0,0 @@
1
- %h1
2
- Editing
3
- = resource.class.to_s.titleize
4
-
5
- = render 'form'
6
-
7
- = link_to 'Show', resource
8
- \|
9
- = link_to 'Back', collection_path
10
-
@@ -1,23 +0,0 @@
1
- %h1
2
- Listing
3
- = collection.name
4
-
5
- %table
6
- %tr
7
- - shown_attrs.each do |attr|
8
- %th
9
- = attr.titleize
10
- %th
11
- %th
12
- %th
13
-
14
- - collection.each do |res|
15
- %tr
16
- - shown_attrs.each do |attr|
17
- %td= res.send(attr)
18
- %td= link_to 'Show', res
19
- %td= link_to 'Edit', edit_resource_path(res)
20
- %td= link_to 'Destroy', res, :confirm => 'Are you sure?', :method => :delete
21
- %br
22
- = link_to "Create #{collection.name}", new_resource_path
23
-
@@ -1,8 +0,0 @@
1
- %h1
2
- New
3
- = resource.class.to_s.titleize
4
-
5
- = render 'form'
6
-
7
- = link_to 'Back', collection_path
8
-
@@ -1,15 +0,0 @@
1
- %p#notice= notice
2
-
3
- %table
4
- - shown_attrs.each do |attr|
5
- %tr
6
- %td
7
- %b
8
- = attr.titleize
9
- %td
10
- = resource.send attr
11
-
12
- = link_to 'Edit', edit_resource_path(resource)
13
- \|
14
- = link_to 'Back', collection_path
15
-