easy_presenter 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b0a86beeb7a5bcf06f9eba3edf7a1def8cfd4269
4
- data.tar.gz: 7c8a99589f8221df2e7d1f6f3723eb691d94f491
3
+ metadata.gz: 557423830a92670748587d61ad8b3043887544ad
4
+ data.tar.gz: 56837121f14983f07e3299e869cd9d5ff2e9103a
5
5
  SHA512:
6
- metadata.gz: d7ada92812250dd0e5824e473ab18dd001136d568fd9ab6bd2075a09c3445bc7a635bc88fcc94a19516a687fea2bea295c8209f34e401464513156454c23bc0e
7
- data.tar.gz: 2fea4d3703aaade50bbfd077d79c9d9b64a48de757b728a9459e9bcdcdf0c874bddc9507c91dd47a22e99305c35174599899de31f118e188c03397f0779abe88
6
+ metadata.gz: fa8ce1aa1f80c41da366164a6b9f0af293c9a893d80a135112f8c3346a4c0726285ea77af454f6d2e14670c54edc7a38af18bdb5cd22404e65e706800ee5b97d
7
+ data.tar.gz: 1123d83e8552be4a0e454cd1e4de85082451510ef20a367d6886db38aae18db49da49d347f6ecedb429fec9fddd3fcb9f4d095ee068a17f039e981c5ede310ba
data/.gitignore CHANGED
@@ -15,4 +15,5 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
- .idea/*
18
+ .idea/*
19
+ log/*
data/.travis.yml ADDED
@@ -0,0 +1,21 @@
1
+ language: ruby
2
+ script: bundle exec rspec
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - ruby-head
7
+ gemfile:
8
+ - gemfiles/Gemfile-rails.3.2.x
9
+ - gemfiles/Gemfile-rails.4.0.x
10
+ matrix:
11
+ exclude:
12
+ - rvm: 2.0.0
13
+ gemfile: gemfiles/Gemfile-rails.3.0.x
14
+ - rvm: 2.0.0
15
+ gemfile: gemfiles/Gemfile-rails.3.1.x
16
+ - rvm: ruby-head
17
+ gemfile: gemfiles/Gemfile-rails.3.0.x
18
+ - rvm: ruby-head
19
+ gemfile: gemfiles/Gemfile-rails.3.1.x
20
+ allow_failures:
21
+ - rvm: ruby-head
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # EasyPresenter
1
+ # EasyPresenter [![Build Status](https://travis-ci.org/s12chung/easy_presenter.png?branch=master)](https://travis-ci.org/s12chung/easy_presenter?branch=master) [![Code Climate](https://codeclimate.com/github/s12chung/easy_presenter.png)](https://codeclimate.com/github/s12chung/easy_presenter)
2
2
 
3
- Access view methods from the context of your `Ruby on Rails` model.
3
+ Access view methods from model instances in `Ruby on Rails`.
4
4
 
5
5
  ## Usage
6
6
  ### Easy
@@ -26,8 +26,7 @@ module UserPresenter
26
26
  module ClassMethods
27
27
  # String of all full names separated by commas
28
28
  def full_names
29
- # better to access `User` this way, you may use `User` instead of `self.class` though.
30
- self.class.all.map(&:full_name).join(", ")
29
+ all.map(&:full_name).join(", ")
31
30
  end
32
31
  end
33
32
  end
@@ -57,9 +56,8 @@ end
57
56
  ### Advanced
58
57
  You may `include` the following modules to add EasyPresenter to other classes.
59
58
 
60
- * `easy_presenter_subclass` - for handling subclasses
61
59
  * `EasyPresenter::Base` - add access to view methods
62
- * `EasyPresenter` - `EasyPresenter::Base` and if `method_missing?`, give access to the `ClassNamePresenter` class
60
+ * `EasyPresenter` - Give access to the `ClassNamePresenter` class that has access to view methods. Subclasses will also have the same effect.
63
61
 
64
62
 
65
63
  To add access to view methods to all instances or subclasses (such as an ORM).
@@ -8,9 +8,9 @@ Gem::Specification.new do |spec|
8
8
  spec.version = EasyPresenter::VERSION
9
9
  spec.authors = ["s12chung"]
10
10
  spec.email = ["steve.chung7@gmail.com"]
11
- spec.description = "Access view methods in your models."
12
- spec.summary = "Access view methods in your models."
13
- spec.homepage = ""
11
+ spec.description = "Access view methods from model instances."
12
+ spec.summary = "Access view methods from model instances."
13
+ spec.homepage = "https://github.com/s12chung/easy_presenter"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
@@ -18,8 +18,12 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rake"
23
-
21
+ spec.add_dependency 'rails', '>= 3.2'
24
22
  spec.add_dependency 'request_store'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.3'
25
+ spec.add_development_dependency 'rake'
26
+ spec.add_development_dependency 'rspec-rails'
27
+ spec.add_development_dependency 'capybara'
28
+ spec.add_development_dependency 'sqlite3'
25
29
  end
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'easy_presenter', :path => '..'
4
+
5
+ gem 'rails', '~> 3.2.0'
6
+ gem 'rspec-rails'
7
+ gem 'capybara', '~> 2.0.0'
8
+ gem 'sqlite3'
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'easy_presenter', :path => '..'
4
+
5
+ gem 'rails', '~> 4.0.0'
6
+ gem 'rspec-rails'
7
+ gem 'capybara'
8
+ gem 'sqlite3'
@@ -1,3 +1,3 @@
1
1
  module EasyPresenter
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -6,17 +6,22 @@ module EasyPresenter
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  included do
9
- easy_presenter_subclass
9
+ include_easy_presenter
10
10
  end
11
11
 
12
12
  module ClassMethods
13
+ def inherited(subclass)
14
+ include_easy_presenter(subclass)
15
+ super
16
+ end
17
+
13
18
  protected
14
- def easy_presenter_subclass
15
- presenter_class = "#{self}Presenter".safe_constantize
19
+ def include_easy_presenter(klass=self)
20
+ presenter_class = "#{klass}Presenter".safe_constantize
16
21
  if presenter_class
17
22
  presenter_class.extend ActiveSupport::Concern
18
- include presenter_class
19
- include Base
23
+ klass.send :include, presenter_class
24
+ klass.send :include, Base
20
25
  end
21
26
  presenter_class
22
27
  end
@@ -0,0 +1,30 @@
1
+ class ApplicationController < ActionController::Base
2
+ self.append_view_path File.join(File.dirname(__FILE__), "views")
3
+ end
4
+
5
+ class UsersController < ApplicationController
6
+ def index
7
+ end
8
+
9
+ def show
10
+ @user = User.find params[:id]
11
+ end
12
+
13
+ def new
14
+ @queen = Queen.new
15
+ @prince = Prince.new
16
+ @paper = Paper.new
17
+ end
18
+ end
19
+
20
+ class GamesController < ApplicationController
21
+ def show
22
+ @game = User.find(params[:user_id]).games.find(params[:id])
23
+ end
24
+ end
25
+
26
+ class TreesController < ApplicationController
27
+ def show
28
+ @tree = Tree.find params[:id]
29
+ end
30
+ end
@@ -0,0 +1 @@
1
+ module ApplicationHelper end
@@ -0,0 +1,22 @@
1
+ require 'easy_presenter'
2
+
3
+ class User < ActiveRecord::Base
4
+ has_many :games
5
+ end
6
+ class Game < ActiveRecord::Base
7
+ belongs_to :user
8
+ end
9
+ class Tree < ActiveRecord::Base
10
+ end
11
+
12
+ class Queen
13
+ include EasyPresenter
14
+ end
15
+ class Prince < Queen
16
+ end
17
+ class Paper
18
+ include EasyPresenter::Base
19
+ def link
20
+ link_to "base"
21
+ end
22
+ end
@@ -0,0 +1,37 @@
1
+ module UserPresenter
2
+ def full_name
3
+ "#{first_name} #{last_name}"
4
+ end
5
+ def link
6
+ link_to full_name, user_path(self)
7
+ end
8
+
9
+ module ClassMethods
10
+ def full_names
11
+ all.map(&:full_name).join(", ")
12
+ end
13
+ end
14
+ end
15
+ module QueenPresenter
16
+ def link
17
+ link_to "parent"
18
+ end
19
+ end
20
+
21
+ module PrincePresenter
22
+ def link
23
+ link_to "child"
24
+ end
25
+ end
26
+
27
+ module GamePresenter
28
+ def free?
29
+ name == "Team Fortress 2"
30
+ end
31
+ end
32
+
33
+ module TreePresenter
34
+ def url
35
+ tree_url(self)
36
+ end
37
+ end
@@ -0,0 +1 @@
1
+ <%= @game.free? %>
@@ -0,0 +1 @@
1
+ <%= @tree.url %>
@@ -0,0 +1 @@
1
+ <%= User.full_names %>
@@ -0,0 +1 @@
1
+ <%= instance_variable_get("@#{params[:class]}").link %>
@@ -0,0 +1 @@
1
+ <%= @user.link %>
@@ -0,0 +1,24 @@
1
+ require 'rails'
2
+ require 'action_controller/railtie'
3
+ require 'action_view/railtie'
4
+
5
+ # config
6
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
7
+
8
+ module FakeApp
9
+ class Application < Rails::Application
10
+ config.secret_token = 'Listen to your heart. Be brave. Do what you gotta do.'
11
+ config.session_store :cookie_store, :key => 'session_store'
12
+ config.active_support.deprecation = :log
13
+ config.eager_load = false
14
+ end
15
+ end
16
+ FakeApp::Application.initialize!
17
+
18
+ %w[routes migrations].each do |file|
19
+ require "fake_app/#{file}"
20
+ end
21
+
22
+ %w[presenters models controllers helpers].each do |file|
23
+ require "fake_app/app/#{file}"
24
+ end
@@ -0,0 +1,7 @@
1
+ class SetupMigration < ActiveRecord::Migration
2
+ def self.up
3
+ create_table(:users) {|t| t.string :first_name; t.string :last_name}
4
+ create_table(:games) {|t| t.string :name; t.references :user }
5
+ create_table(:trees) {|t| t.string :name }
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ FakeApp::Application.routes.draw do
2
+ resources :users, :only => [:index, :show, :new] do
3
+ resources :games, :only => [:show]
4
+ end
5
+
6
+ resources :trees, :only => [:show]
7
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe EasyPresenter::Base, :type => :feature do
4
+ before :each do
5
+ @tree = Tree.create(:name => "Sakura")
6
+ end
7
+
8
+ describe "#url_options" do
9
+ it "should allow using Rails #url_for helpers" do
10
+ path = "/trees/#{@tree.id}"
11
+ visit "/trees/#{@tree.id}"
12
+ page.should have_content "http://www.example.com#{"/trees/#{@tree.id}"}"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ feature 'fallback to controller context' do
4
+ background do
5
+ @user = User.create! :first_name => 'Ji-yoon', :last_name => 'Park'
6
+ User.create! :first_name => 'Bret', :last_name => 'Victor'
7
+
8
+ @game = Game.create! :name => "Team Fortress 2"
9
+ @user.games << @game
10
+ end
11
+
12
+ scenario 'invoke basic methods and helpers' do
13
+ visit "/users/#{@user.id}"
14
+ within 'a' do
15
+ page.should have_content 'Ji-yoon Park'
16
+ end
17
+ end
18
+
19
+ scenario 'nested resource' do
20
+ visit "/users/#{@user.id}/games/#{@game.id}"
21
+ page.should have_content 'true'
22
+ end
23
+
24
+ scenario 'collections and class methods' do
25
+ visit "/users"
26
+ page.should have_content 'Ji-yoon Park, Bret Victor'
27
+ end
28
+
29
+ scenario 'not a active record model' do
30
+ visit "/users/new?class=queen"
31
+ within 'a' do
32
+ page.should have_content 'parent'
33
+ end
34
+ end
35
+
36
+ scenario 'child of a active record model' do
37
+ visit "/users/new?class=prince"
38
+ within 'a' do
39
+ page.should have_content 'child'
40
+ end
41
+ end
42
+
43
+ scenario 'including just base' do
44
+ visit "/users/new?class=paper"
45
+ within 'a' do
46
+ page.should have_content 'base'
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,24 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'active_record'
5
+
6
+ require 'request_store'
7
+ require 'easy_presenter'
8
+
9
+ require 'fake_app/fake_app'
10
+
11
+ require 'rspec/rails'
12
+
13
+ RSpec.configure do |config|
14
+ config.before :all do
15
+ unless ActiveRecord::Base.connection.table_exists? 'users'
16
+ SetupMigration.up
17
+ end
18
+ end
19
+ config.before :each do
20
+ %w[User Game Tree].each do |klass|
21
+ klass.constantize.delete_all
22
+ end
23
+ end
24
+ end
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_presenter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - s12chung
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-22 00:00:00.000000000 Z
11
+ date: 2013-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: request_store
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: bundler
15
43
  requirement: !ruby/object:Gem::Requirement
@@ -39,20 +67,48 @@ dependencies:
39
67
  - !ruby/object:Gem::Version
40
68
  version: '0'
41
69
  - !ruby/object:Gem::Dependency
42
- name: request_store
70
+ name: rspec-rails
43
71
  requirement: !ruby/object:Gem::Requirement
44
72
  requirements:
45
73
  - - '>='
46
74
  - !ruby/object:Gem::Version
47
75
  version: '0'
48
- type: :runtime
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: capybara
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
49
105
  prerelease: false
50
106
  version_requirements: !ruby/object:Gem::Requirement
51
107
  requirements:
52
108
  - - '>='
53
109
  - !ruby/object:Gem::Version
54
110
  version: '0'
55
- description: Access view methods in your models.
111
+ description: Access view methods from model instances.
56
112
  email:
57
113
  - steve.chung7@gmail.com
58
114
  executables: []
@@ -60,17 +116,35 @@ extensions: []
60
116
  extra_rdoc_files: []
61
117
  files:
62
118
  - .gitignore
119
+ - .travis.yml
63
120
  - Gemfile
64
121
  - LICENSE.txt
65
122
  - README.md
66
123
  - Rakefile
67
124
  - easy_presenter.gemspec
125
+ - gemfiles/Gemfile-rails.3.2.x
126
+ - gemfiles/Gemfile-rails.4.0.x
68
127
  - lib/easy_presenter.rb
69
128
  - lib/easy_presenter/base.rb
70
129
  - lib/easy_presenter/filter.rb
71
130
  - lib/easy_presenter/railtie.rb
72
131
  - lib/easy_presenter/version.rb
73
- homepage: ''
132
+ - spec/fake_app/app/controllers.rb
133
+ - spec/fake_app/app/helpers.rb
134
+ - spec/fake_app/app/models.rb
135
+ - spec/fake_app/app/presenters.rb
136
+ - spec/fake_app/app/views/games/show.html.erb
137
+ - spec/fake_app/app/views/trees/show.html.erb
138
+ - spec/fake_app/app/views/users/index.html.erb
139
+ - spec/fake_app/app/views/users/new.html.erb
140
+ - spec/fake_app/app/views/users/show.html.erb
141
+ - spec/fake_app/fake_app.rb
142
+ - spec/fake_app/migrations.rb
143
+ - spec/fake_app/routes.rb
144
+ - spec/features/easy_presenter/base_spec.rb
145
+ - spec/features/easy_presenter_spec.rb
146
+ - spec/spec_helper.rb
147
+ homepage: https://github.com/s12chung/easy_presenter
74
148
  licenses:
75
149
  - MIT
76
150
  metadata: {}
@@ -93,5 +167,20 @@ rubyforge_project:
93
167
  rubygems_version: 2.0.3
94
168
  signing_key:
95
169
  specification_version: 4
96
- summary: Access view methods in your models.
97
- test_files: []
170
+ summary: Access view methods from model instances.
171
+ test_files:
172
+ - spec/fake_app/app/controllers.rb
173
+ - spec/fake_app/app/helpers.rb
174
+ - spec/fake_app/app/models.rb
175
+ - spec/fake_app/app/presenters.rb
176
+ - spec/fake_app/app/views/games/show.html.erb
177
+ - spec/fake_app/app/views/trees/show.html.erb
178
+ - spec/fake_app/app/views/users/index.html.erb
179
+ - spec/fake_app/app/views/users/new.html.erb
180
+ - spec/fake_app/app/views/users/show.html.erb
181
+ - spec/fake_app/fake_app.rb
182
+ - spec/fake_app/migrations.rb
183
+ - spec/fake_app/routes.rb
184
+ - spec/features/easy_presenter/base_spec.rb
185
+ - spec/features/easy_presenter_spec.rb
186
+ - spec/spec_helper.rb