Skip to content
This repository has been archived by the owner on Dec 17, 2018. It is now read-only.

Commit

Permalink
add feb_meta method to application controller to allow setting custom…
Browse files Browse the repository at this point in the history
… meta tags
  • Loading branch information
ngottlieb committed Sep 22, 2015
1 parent 42e089b commit 1396ab6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
16 changes: 16 additions & 0 deletions app/controllers/front_end_builds/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,21 @@ def error!(errors, status = :unprocessable_entity)
respond_with_json({ errors: errors }, status: status)
end

# Override this method using a decorator to customize what meta tags are
# injected into index.html. Most uses cases will probably want to copy
# these default values into their method and add to them rather than
# just replace them.
def feb_meta(app)

This comment has been minimized.

Copy link
@ryanto

ryanto Sep 23, 2015

Should feb_meta be part of the builds_controller? Seems like only builds controller would use this.

This comment has been minimized.

Copy link
@ngottlieb

ngottlieb Sep 23, 2015

Author Owner

I can move it there -- or maybe just into the BestsController since that's where it's actually called?

This comment has been minimized.

Copy link
@ryanto

ryanto Sep 23, 2015

Sorry you're right, BestsController.

{
csrf_param: request_forgery_protection_token,
csrf_token: form_authenticity_token,
front_end_build_version: app.id,
front_end_build_params: use_params(:build_search_params).to_query,
front_end_build_url: front_end_builds_best_path(
use_params(:build_search_params).merge(format: :json)
)
}
end

end
end
10 changes: 1 addition & 9 deletions app/controllers/front_end_builds/bests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,7 @@ def show
private

def meta_tags
tags = {
csrf_param: request_forgery_protection_token,
csrf_token: form_authenticity_token,
front_end_build_version: @front_end.id,
front_end_build_params: use_params(:build_search_params).to_query,
front_end_build_url: front_end_builds_best_path(
use_params(:build_search_params).merge(format: :json)
)
}
tags = feb_meta(@front_end)

tags
.map { |name, content|
Expand Down
15 changes: 14 additions & 1 deletion spec/controllers/front_end_builds/bests_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module FrontEndBuilds
expect(response.body).to match(latest.html)
end

context "meta tags" do
context "default meta tags" do
before(:each) do
get :show, app_name: app.name, branch: 'master'
expect(response).to be_success
Expand All @@ -76,6 +76,19 @@ module FrontEndBuilds
it { should match(/front-end-build-url/) }
end

context "custom meta tags" do
before(:each) do
allow(controller).to receive(:feb_meta)
.and_return({ fake_meta_tag: 'custom' })
get :show, app_name: app.name, branch: 'master'
expect(response).to be_success
end

subject { response.body }

it { should match(/fake-meta-tag/) }
end

it "should be 404 when nothing is found" do
get :show, app_name: 'does-not-exist', branch: 'master'
expect(response).to_not be_success
Expand Down

2 comments on commit 1396ab6

@ryanto
Copy link

@ryanto ryanto commented on 1396ab6 Sep 23, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this. Looks good!

Do you mind adding a section to the README.md about how a developer can add their own meta tags?

Also, I assume you'll monkey patch feb_meta to get custom meta. That's fine for version 1, any thoughts on the next evolution of the API?

Thanks so much for adding this

@ngottlieb
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NP -- and, will do. Yeah, the way I'm using this now is to use a decorator to override feb_meta. I think it would be pretty cool to incorporate custom meta attributes into the models and UI, personally, but I could see arguments against that, too. We have relatively non-technical people using the admin interface and allowing people to change configuration variables without changing any code would be sweet.

It's a slightly different feature, but I also like the ideas mentioned in tedconf#33 for exposing a low-level hook that allows you to take custom actions on the entire HTML before it's presented. I'd like to see this handled in a way that doesn't require monkey-patching with a decorator, but the only really nice way around that I've encountered is the way Devise handles it which involves a lot of extra overhead.

Please sign in to comment.