great_pretender 0.0.2 → 0.1.1

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: b0a84b26997b6eb8cf255e8f687a1b1b6238ce70
4
- data.tar.gz: 5f7d90db4cf3dabd4c410d413237da15360d5e28
3
+ metadata.gz: 83426688dbe661c6888971ba60756bd22a22d8e9
4
+ data.tar.gz: 609888f148e55a50216994b642adb78527f8f56f
5
5
  SHA512:
6
- metadata.gz: 2c6e1dcc3080c725c1e1405346aad7f1b130b6b9dbc2504804ade1c5a535545379d3e4876ad6fc4ee8f00967619ee7bcf3d724b66d1b40f3fbf61a62a9ef350e
7
- data.tar.gz: 3071b731a1bca2a49b3d97d7544221cf56bd5cd6bfc2908c4144ad4a358aef585422e811af35855ed3055389b84e7b9d7421316b727c5a7d804c336320142599
6
+ metadata.gz: 3a87473df668fc6b002039e804e07aa0dfd3d93ed8e60aa671ca4077439e13795c0baa92409de6dec5d7b8c8dd3fa29dbc0a6c24e8afe058b44d88e203a18dc5
7
+ data.tar.gz: 416e2559c885fe04eb3a55a109e57b38a93944886dac8a1f8c8a734912b38654c263d6be76b03ada883f15d9d475f50f9c12d393c96f31449603674cbcd0c87c
data/Gemfile CHANGED
@@ -2,3 +2,4 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in great_pretender.gemspec
4
4
  gemspec
5
+ gem "rspec-rails"
data/README.md CHANGED
@@ -6,6 +6,7 @@
6
6
 
7
7
  **Great Pretender** was designed to be easy and awesome. Here are a few of its neat features:
8
8
 
9
+ - Allows for easy stubbing with test data
9
10
  - Supports multiple layouts
10
11
  - Supports nested mockup directories (e.g. `app/views/mockups/users/sessions/whatever.html.slim`)
11
12
  - Supports partials
@@ -100,15 +101,13 @@ If that's the case, you can create your own controller that uses Great Pretender
100
101
 
101
102
  Here's an example:
102
103
 
103
- ```
104
+ ```haml
104
105
  %ul
105
106
  - mockups.each do |mockup|
106
107
  %li= link_to mockup.name, admin_mockup_path(mockup)
107
108
  ```
108
109
 
109
- ## Usage
110
-
111
- ### Creating mockups
110
+ ## Creating Mockups
112
111
 
113
112
  Once you've got Great Pretender up and running, you should add some mockups. By default, you would put these in `app/views/mockups`, but if you overrode `view_path` in `GreatPretender.config.view_path`, you would use `app/views/whatever_you_overrode_it_to`.
114
113
 
@@ -116,18 +115,18 @@ Once you've got Great Pretender up and running, you should add some mockups. By
116
115
 
117
116
  For example, in `app/views/mockups/users/show.html.slim`:
118
117
 
119
- ```
118
+ ```slim
120
119
  header= image_tag "logo.png"
121
120
  p
122
121
  | I can use regular Rails helpers because this is just a template file!
123
122
  ' Hooray!
124
123
  ```
125
124
 
126
- 2. Open your browser and navigate to whatever path you installed Great Pretender on (90% of cases I'm guessing will be in `http://localhost:3000/mockups`). Then click on your mockup.
125
+ 2. Open your browser and navigate to whatever path you installed Great Pretender on (90% of cases I'm guessing will be at [http://localhost:3000/mockups](http://localhost:3000/mockups)). Then click on your mockup.
127
126
 
128
127
  3. Profit
129
128
 
130
- ### Creating partials
129
+ ### Partials
131
130
 
132
131
  You can add partials like you would anywhere else. Just prefix them with a skid (`_`) and go from there.
133
132
 
@@ -135,7 +134,7 @@ By default, partials render **without a layout**, so you can request them using
135
134
 
136
135
  If you *want* to receive a layout as part of it, use a custom layout (see next section) and just request that mockups' path.
137
136
 
138
- ### Using alternative layouts
137
+ ### Multiple layout support
139
138
 
140
139
  If you're designing for a site with multiple layouts, you can simply nest mockup files in a folder with the name of a layout, and they'll be rendered with that layout. For example, with the following structure...
141
140
 
@@ -156,6 +155,54 @@ app/
156
155
 
157
156
  Using this strategy, you can name your mockups clearly after their conceptual section in your app.
158
157
 
158
+ ## Rendering With Test Data (Pretender Objects)
159
+
160
+ A lot of the time you'll want to emulate real data in your views. In those cases, Great Pretender supports the use of "Pretender" objects in `app/pretenders`.
161
+
162
+ Pretender objects can be any class. They can accept a single argument in `initialize`, which will be the the mockup being rendered, **or** they can skip it entirely.
163
+
164
+ Any public method you add to a pretender object will be available to the view, just like helper methods.
165
+
166
+ ### Load order
167
+
168
+ Pretenders, like layouts, are loaded based on the name of your mockup file. For example, a mockup named "users/login" would look for `app/pretenders/user(s)_pretender.rb` and `app/pretenders/login_pretender.rb`. **Please note** that plurally named templates will support a singular pretender, but singularly named templates will only load singular pretenders.
169
+
170
+ Pretenders are delegated to in order of most specific (`LoginPretender`) to least specific (`UserPretender`). So if `LoginPretender` and `UserPretender` both define method `name`, the `LoginPretender`'s implementation will be used.
171
+
172
+ ### Example
173
+
174
+ Here's an example using the excellent [Faker gem](https://github.com/stympy/faker) (I leave it to you to figure out how you want to generate test data, but Faker is pretty awesome).
175
+
176
+ #### `app/pretenders/user_pretender.rb`
177
+
178
+ ```ruby
179
+ require "faker"
180
+
181
+ class UserPretender
182
+
183
+ def login
184
+ Faker::Internet.user_name
185
+ end
186
+
187
+ def email
188
+ Faker::Internet.email
189
+ end
190
+
191
+ end
192
+ ```
193
+
194
+ #### `app/views/mockups/users/login.html.slim`
195
+
196
+
197
+ ```slim
198
+ header
199
+ | Welome back, #{login}!
200
+ | Your email is set to #{email}
201
+
202
+ / The remainder of your login form mockup...
203
+ ```
204
+
205
+
159
206
  ## The end!
160
207
 
161
208
  Thanks for using Great Pretender! I hope it helps you like it's helped us!
@@ -1,4 +1,5 @@
1
1
  require "great_pretender/mockup_locator"
2
+ require "great_pretender/pretender"
2
3
 
3
4
  module GreatPretender
4
5
  module Controller
@@ -41,5 +42,15 @@ module GreatPretender
41
42
  @great_pretender_mockup_root ||= mockup_locator.view_paths.first.join(GreatPretender.config.view_path)
42
43
  end
43
44
 
45
+ def pretender
46
+ @great_pretender_pretender ||= Pretender.new(mockup)
47
+ end
48
+
49
+ def view_context
50
+ super.tap do |view_context|
51
+ view_context.extend pretender.to_module if mockup
52
+ end
53
+ end
54
+
44
55
  end
45
56
  end
@@ -1,4 +1,9 @@
1
1
  module GreatPretender
2
2
  class Engine < ::Rails::Engine
3
+ config.generators do |g|
4
+ g.test_framework :rspec
5
+ end
6
+
7
+ config.autoload_paths.push(Pathname.new('').join('app', 'pretenders').expand_path.to_s)
3
8
  end
4
9
  end
@@ -0,0 +1,62 @@
1
+ require "active_support/core_ext/string/inflections"
2
+
3
+ module GreatPretender
4
+ class Pretender
5
+
6
+ def initialize(mockup)
7
+ @mockup = mockup
8
+ end
9
+
10
+ def to_module
11
+ pretenders = load_pretenders
12
+
13
+ Module.new do
14
+ define_method(:method_missing) do |method_name, *args, &block|
15
+ pretender = pretenders.find { |p| p.respond_to?(method_name) }
16
+
17
+ if pretender
18
+ pretender.send(method_name, *args, &block)
19
+ else
20
+ super(method_name, *args, &block)
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def load_pretender(class_name)
29
+ class_name = class_name + 'Pretender'
30
+ begin
31
+ klass = class_name.constantize
32
+ rescue NameError
33
+ return nil
34
+ end
35
+
36
+ initialize = klass.instance_method(:initialize)
37
+
38
+ if initialize.arity == 1
39
+ klass.new(@mockup)
40
+ else
41
+ klass.new
42
+ end
43
+ end
44
+
45
+ def load_pretenders
46
+ pretenders = []
47
+ @mockup.slug.split("/").each do |pretender_name|
48
+ # Given a mockup named something like 'social_contents',
49
+ # support pretenders named 'SocialContentsPretender' AND 'SocialContentPretender'
50
+ first_class = pretender_name.classify
51
+ second_class = pretender_name.split("_").map(&:titleize).join
52
+ [first_class, second_class].uniq.each do |class_name|
53
+ if instance = load_pretender(class_name)
54
+ pretenders.push(instance)
55
+ end
56
+ end
57
+ end
58
+ pretenders.reverse
59
+ end
60
+
61
+ end
62
+ end
@@ -1,3 +1,3 @@
1
1
  module GreatPretender
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,33 @@
1
+ require 'great_pretender/pretender'
2
+ require 'ostruct'
3
+
4
+ class PrefixSlugPretender
5
+ def say_hello
6
+ "Hello, guest!"
7
+ end
8
+ end
9
+
10
+ class TestSlugPretender
11
+ def ohai
12
+ "ohai"
13
+ end
14
+ end
15
+
16
+ describe GreatPretender::Pretender do
17
+
18
+ let(:mockup) { OpenStruct.new(slug: "prefix_slug/test_slug") }
19
+ let(:pretender) { GreatPretender::Pretender.new(mockup) }
20
+ let(:recipient) do
21
+ mod = pretender.to_module
22
+ Class.new { include mod }.new
23
+ end
24
+
25
+ it "delegates methods to pretenders named after mockups" do
26
+ expect(recipient.ohai).to eq("ohai")
27
+ end
28
+
29
+ it "delegates methods to pretenders in the slug chain" do
30
+ expect(recipient.say_hello).to eq("Hello, guest!")
31
+ end
32
+
33
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: great_pretender
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Flip Sasser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-07 00:00:00.000000000 Z
11
+ date: 2014-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -65,6 +65,7 @@ files:
65
65
  - lib/great_pretender/engine.rb
66
66
  - lib/great_pretender/mockup.rb
67
67
  - lib/great_pretender/mockup_locator.rb
68
+ - lib/great_pretender/pretender.rb
68
69
  - lib/great_pretender/version.rb
69
70
  - spec/fixtures/view_path_a/.DS_Store
70
71
  - spec/fixtures/view_path_a/layouts/admin.html.slim
@@ -76,6 +77,7 @@ files:
76
77
  - spec/lib/great_pretender/config_spec.rb
77
78
  - spec/lib/great_pretender/mockup_locator_spec.rb
78
79
  - spec/lib/great_pretender/mockup_spec.rb
80
+ - spec/lib/great_pretender/pretender_spec.rb
79
81
  - spec/support/mockup_helpers.rb
80
82
  homepage: https://github.com/BackForty/great_pretender
81
83
  licenses:
@@ -113,4 +115,5 @@ test_files:
113
115
  - spec/lib/great_pretender/config_spec.rb
114
116
  - spec/lib/great_pretender/mockup_locator_spec.rb
115
117
  - spec/lib/great_pretender/mockup_spec.rb
118
+ - spec/lib/great_pretender/pretender_spec.rb
116
119
  - spec/support/mockup_helpers.rb