detfis-rails-parts 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +8 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +52 -0
- data/Rakefile +39 -0
- data/lib/generators/part_generator.rb +21 -0
- data/lib/generators/templates/part.rb +7 -0
- data/lib/parts.rb +3 -0
- data/lib/parts/base.rb +28 -0
- data/lib/parts/default_layout.rb +8 -0
- data/lib/parts/helpers.rb +19 -0
- data/lib/parts/railtie.rb +40 -0
- metadata +67 -0
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2010 YOURNAME
|
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,52 @@
|
|
1
|
+
= Rails Parts
|
2
|
+
|
3
|
+
Merb parts ported to rails.
|
4
|
+
|
5
|
+
As it's initial and a bit experimental implementation please note that API can change (currently it's copied from Merb)
|
6
|
+
|
7
|
+
== Install
|
8
|
+
|
9
|
+
Just add such line to Gemfile:
|
10
|
+
|
11
|
+
gem "rails-parts", :require => "parts"
|
12
|
+
|
13
|
+
and run:
|
14
|
+
|
15
|
+
bundle install
|
16
|
+
|
17
|
+
== Usage
|
18
|
+
|
19
|
+
Generate part class using rails generator:
|
20
|
+
|
21
|
+
rails generate part Articles index
|
22
|
+
|
23
|
+
Add some logic to index method of your part:
|
24
|
+
|
25
|
+
# app/parts/articles_part.rb
|
26
|
+
class ArticlesPart < Parts::Base
|
27
|
+
def index
|
28
|
+
@articles = Article.limit(10)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
and to the view file linked to the part
|
33
|
+
|
34
|
+
# app/parts/views/articles_part/index.html.erb
|
35
|
+
Articles: <%= @article.map(&:title).join(", ") %>
|
36
|
+
|
37
|
+
Now you can render it in the view of any controller with:
|
38
|
+
|
39
|
+
<%= part ArticlesPart => :index %>
|
40
|
+
|
41
|
+
You can also attach params that will be available in Part as <code>params</code> hash:
|
42
|
+
|
43
|
+
part ArticlesPart => :index, :limit => 5
|
44
|
+
|
45
|
+
# app/parts/articles_part.rb
|
46
|
+
class ArticlesPart < Parts::Base
|
47
|
+
def index
|
48
|
+
@articles = Article.limit(params[:limit] || 10)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'rake/gempackagetask'
|
5
|
+
|
6
|
+
require 'rake/testtask'
|
7
|
+
|
8
|
+
Rake::TestTask.new(:test) do |t|
|
9
|
+
t.libs << 'lib'
|
10
|
+
t.libs << 'test'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = false
|
13
|
+
end
|
14
|
+
|
15
|
+
task :default => :test
|
16
|
+
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'Parts'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README.rdoc')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
24
|
+
|
25
|
+
spec = Gem::Specification.new do |s|
|
26
|
+
s.name = "parts"
|
27
|
+
s.summary = "Insert Parts summary."
|
28
|
+
s.description = "Insert Parts description."
|
29
|
+
s.files = FileList["[A-Z]*", "lib/**/*"]
|
30
|
+
s.version = "0.0.1"
|
31
|
+
end
|
32
|
+
|
33
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
34
|
+
end
|
35
|
+
|
36
|
+
desc "Install the gem #{spec.name}-#{spec.version}.gem"
|
37
|
+
task :install do
|
38
|
+
system("gem install pkg/#{spec.name}-#{spec.version}.gem --no-ri --no-rdoc")
|
39
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rails/generators/base'
|
2
|
+
require 'rails/generators/named_base'
|
3
|
+
|
4
|
+
class PartGenerator < Rails::Generators::NamedBase
|
5
|
+
source_root File.expand_path("../templates", __FILE__)
|
6
|
+
|
7
|
+
argument :actions, :type => :array, :default => [], :banner => "action action"
|
8
|
+
check_class_collision :suffix => "Part"
|
9
|
+
|
10
|
+
def generate_part_class
|
11
|
+
template "part.rb", "app/parts/#{file_name}_part.rb"
|
12
|
+
end
|
13
|
+
|
14
|
+
def create_part_views
|
15
|
+
inside("app/parts/views/#{file_name}_part") do
|
16
|
+
actions.each do |action|
|
17
|
+
create_file "#{action}.html.erb"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/parts.rb
ADDED
data/lib/parts/base.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'parts/default_layout'
|
2
|
+
|
3
|
+
module Parts
|
4
|
+
class Base < AbstractController::Base
|
5
|
+
attr_reader :params
|
6
|
+
|
7
|
+
include AbstractController::Layouts
|
8
|
+
include AbstractController::Translation
|
9
|
+
include ActionController::Helpers
|
10
|
+
include AbstractController::Rendering
|
11
|
+
include ActionController::ImplicitRender
|
12
|
+
include DefaultLayout
|
13
|
+
include AbstractController::Callbacks
|
14
|
+
|
15
|
+
def initialize(controller, params)
|
16
|
+
@params = controller.params.dup
|
17
|
+
@params.merge!(params) unless params.empty?
|
18
|
+
self.formats = controller.formats
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.inherited(klass)
|
22
|
+
super
|
23
|
+
klass.helper :all
|
24
|
+
end
|
25
|
+
|
26
|
+
ActiveSupport.run_load_hooks(:parts, self)
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Parts
|
2
|
+
module Helpers
|
3
|
+
def part(opts = {})
|
4
|
+
klasses, opts = opts.partition do |k,v|
|
5
|
+
k.respond_to?(:ancestors) && k.ancestors.include?(Parts::Base)
|
6
|
+
end
|
7
|
+
|
8
|
+
opts = opts.inject({}) {|h,v| h[v.first] = v.last; h}
|
9
|
+
|
10
|
+
res = klasses.inject([]) do |memo,(klass,action)|
|
11
|
+
part = klass.new(self, opts)
|
12
|
+
part.process(action)
|
13
|
+
memo << part.response_body
|
14
|
+
end
|
15
|
+
|
16
|
+
res.size == 1 ? res[0] : res
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "parts"
|
2
|
+
require 'rails'
|
3
|
+
begin
|
4
|
+
require "abstract_controller/railties/routes_helpers"
|
5
|
+
rescue LoadError
|
6
|
+
# not on edge :(
|
7
|
+
end
|
8
|
+
|
9
|
+
module Parts
|
10
|
+
class Railtie < Rails::Railtie
|
11
|
+
initializer "parts.include_helpers" do |app|
|
12
|
+
ActiveSupport.on_load(:action_controller) do
|
13
|
+
ActionView::Base.send(:include, Parts::Helpers)
|
14
|
+
ActionController::Base.send(:include, Parts::Helpers)
|
15
|
+
end
|
16
|
+
|
17
|
+
ActiveSupport.on_load(:parts) do
|
18
|
+
if app.routes.respond_to?(:mounted_helpers)
|
19
|
+
include app.routes.mounted_helpers
|
20
|
+
extend ::AbstractController::Railties::RoutesHelpers.with(app.routes)
|
21
|
+
else
|
22
|
+
include app.routes.url_helpers
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
initializer "parts.set_paths" do |app|
|
28
|
+
paths = app.config.paths
|
29
|
+
paths.app.parts 'app/parts', :eager_load => true
|
30
|
+
paths.app.parts.views 'app/parts/views'
|
31
|
+
|
32
|
+
paths.app.parts.views.to_a.each do |path|
|
33
|
+
Parts::Base.append_view_path path
|
34
|
+
end
|
35
|
+
|
36
|
+
Parts::Base.helpers_path = paths.app.helpers.to_a.first
|
37
|
+
Parts::Base.config[:assets_dir] = ActionController::Base.config.assets_dir
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: detfis-rails-parts
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.3
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Piotr Sarnacki
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-06 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: &70163069675260 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70163069675260
|
25
|
+
description: This gem allows you to use parts in your rails app
|
26
|
+
email:
|
27
|
+
- drogus@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- lib/generators/part_generator.rb
|
33
|
+
- lib/generators/templates/part.rb
|
34
|
+
- lib/parts/base.rb
|
35
|
+
- lib/parts/default_layout.rb
|
36
|
+
- lib/parts/helpers.rb
|
37
|
+
- lib/parts/railtie.rb
|
38
|
+
- lib/parts.rb
|
39
|
+
- MIT-LICENSE
|
40
|
+
- README.rdoc
|
41
|
+
- Rakefile
|
42
|
+
- Gemfile
|
43
|
+
homepage: http://github.com/drogus/rails-parts
|
44
|
+
licenses: []
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 1.3.6
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.8.10
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Merb parts ported to rails
|
67
|
+
test_files: []
|