Skip to content

Commit eb8b96f

Browse files
authored
Merge pull request #179 from joyofrails/feat/example-post
Add Example Post table and delegated types
2 parents 5bc5156 + 6f79710 commit eb8b96f

31 files changed

+608
-2
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Examples::PostsController < ApplicationController
2+
before_action :feature_enabled!
3+
4+
def index
5+
render Examples::Posts::IndexView.new(posts: Examples::Post.limit(25).order(created_at: :desc))
6+
end
7+
8+
def new
9+
post = Examples::Post.new(post_params_permitted)
10+
11+
render Examples::Posts::NewView.new(post: post)
12+
end
13+
14+
def create
15+
post = Examples::Post.new(post_params_permitted)
16+
17+
if post.save
18+
redirect_to examples_posts_path
19+
else
20+
render Examples::Posts::NewView.new(post: post), status: :unprocessable_entity
21+
end
22+
end
23+
24+
private
25+
26+
def post_params = params.fetch(:examples_post, {})
27+
28+
def post_params_permitted
29+
post_params.permit(:title, :postable_type, link_attributes: [:url], image_attributes: [:url], markdown_attributes: [:body])
30+
end
31+
32+
def feature_enabled!
33+
return if user_signed_in? &&
34+
Flipper.enabled?(:example_posts, current_user)
35+
36+
raise ActionController::RoutingError.new("Not Found")
37+
end
38+
end

app/controllers/users/sessions_controller.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ def fail
4545
private
4646

4747
def feature_enabled!
48-
redirect_to root_path, notice: "Coming soon!" unless Flipper[:user_registration].enabled?
48+
unless Flipper.enabled?(:user_registration, current_admin_user)
49+
redirect_to root_path,
50+
notice: "Coming soon!"
51+
end
4952
end
5053
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Controller } from '@hotwired/stimulus';
2+
import debug from '../../utils/debug';
3+
4+
const console = debug('app:javascript:controllers:forms:frame');
5+
6+
export default class extends Controller {
7+
static targets = ['refreshButton'];
8+
static values = {
9+
redirectFrame: String,
10+
};
11+
12+
connect() {
13+
console.log('Connect!');
14+
this.refreshButtonTarget.hidden = true;
15+
}
16+
17+
refresh() {
18+
console.log('Refresh!');
19+
this.refreshButtonTarget.click();
20+
}
21+
22+
redirect(event) {
23+
if (this.refreshButtonTarget === event.detail.formSubmission.submitter)
24+
return;
25+
26+
if (event.detail.success) {
27+
const fetchResponse = event.detail.fetchResponse;
28+
history.pushState(
29+
{ turbo_frame_history: true },
30+
'',
31+
fetchResponse.response.url,
32+
);
33+
console.log('Redirect!', fetchResponse.response.url, {
34+
frame: this.redirectFrameValue,
35+
});
36+
Turbo.visit(fetchResponse.response.url, {
37+
frame: this.redirectFrameValue,
38+
});
39+
}
40+
}
41+
}

app/javascript/controllers/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import PwaWebPushDemo from './pwa/web-push-demo';
1515
import AnalyticsCustomEvent from './analytics/custom-event';
1616
import TableOfContents from './table-of-contents';
1717

18+
import FrameForm from './forms/frame';
19+
1820
application.register('analytics', AnalyticsCustomEvent);
1921
application.register('code-example', CodeExample);
2022
application.register('clipboard-copy', ClipboardCopy);
@@ -24,3 +26,5 @@ application.register('pwa-installation', PwaInstallation);
2426
application.register('pwa-web-push-subscription', PwaWebPushSubscription);
2527
application.register('pwa-web-push-demo', PwaWebPushDemo);
2628
application.register('table-of-contents', TableOfContents);
29+
30+
application.register('frame-form', FrameForm);

app/javascript/css/components/button.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
font-family: var(--sans-serif);
1010
color: var(--joy-light);
1111

12+
&[hidden] {
13+
display: none;
14+
}
15+
1216
&:is(a) {
1317
text-decoration: none;
1418
}

app/models/examples.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Examples
2+
def self.table_name_prefix
3+
"examples_"
4+
end
5+
end

app/models/examples/post.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class Examples::Post < ApplicationRecord
2+
POSTABLE_TYPES = %w[
3+
Examples::Posts::Markdown
4+
Examples::Posts::Link
5+
Examples::Posts::Image
6+
].freeze
7+
8+
delegated_type :postable, types: POSTABLE_TYPES
9+
10+
attr_writer :markdown, :link, :image
11+
12+
accepts_nested_attributes_for :postable
13+
14+
after_initialize :infer_postable
15+
16+
def self.postable_types
17+
POSTABLE_TYPES
18+
end
19+
20+
def markdown
21+
@markdown || (postable if postable.is_a?(Examples::Posts::Markdown))
22+
end
23+
24+
def image
25+
@image || (postable if postable.is_a?(Examples::Posts::Image))
26+
end
27+
28+
def link
29+
@link || (postable if postable.is_a?(Examples::Posts::Link))
30+
end
31+
32+
def markdown?
33+
@markdown.present? || postable.is_a?(Examples::Posts::Markdown)
34+
end
35+
36+
def image?
37+
@image.present? || postable.is_a?(Examples::Posts::Image)
38+
end
39+
40+
def link?
41+
@link.present? || postable.is_a?(Examples::Posts::Link)
42+
end
43+
44+
def markdown_attributes=(attributes)
45+
self.markdown = Examples::Posts::Markdown.new(attributes)
46+
end
47+
48+
def link_attributes=(attributes)
49+
self.link = Examples::Posts::Link.new(attributes)
50+
end
51+
52+
def image_attributes=(attributes)
53+
self.image = Examples::Posts::Image.new(attributes)
54+
end
55+
56+
private
57+
58+
def infer_postable
59+
return unless postable.nil?
60+
61+
case postable_type
62+
when "Examples::Posts::Markdown"
63+
self.postable = markdown if markdown?
64+
when "Examples::Posts::Link"
65+
self.postable = link if link?
66+
when "Examples::Posts::Image"
67+
self.postable = image if image?
68+
end
69+
end
70+
end

app/models/examples/posts.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Examples::Posts
2+
def self.table_name_prefix
3+
"examples_posts_"
4+
end
5+
end

app/models/examples/posts/image.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Examples::Posts::Image < ApplicationRecord
2+
end

app/models/examples/posts/link.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Examples::Posts::Link < ApplicationRecord
2+
end

0 commit comments

Comments
 (0)