Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements our StyleGuide #7284

Merged
merged 12 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ coverage
/reports/
!/reports/README.md
bin/
/spec/components/stories/**/*.stories.json
7 changes: 7 additions & 0 deletions .storybook/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
stories: ['../spec/components/stories/**/*.stories.json'],
addons: [
'@storybook/addon-docs',
'@storybook/addon-controls',
],
};
2 changes: 2 additions & 0 deletions .storybook/preview-head.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<link href='https://fonts.googleapis.com/css?family=Roboto:400,300italic,400italic,300,700,700italic|Oswald:300,400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" media="screen" href="http://localhost:3000/assets/darkswarm/all.css" />
5 changes: 5 additions & 0 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const parameters = {
server: {
url: `http://localhost:3000/rails/stories`,
},
};
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ gem 'flipper'
gem 'flipper-active_record'
gem 'flipper-ui'

gem "view_component", require: "view_component/engine"

group :production, :staging do
gem 'ddtrace'
gem 'unicorn-worker-killer'
Expand Down Expand Up @@ -164,6 +166,8 @@ group :development do
gem 'spring'
gem 'spring-commands-rspec'

gem "view_component_storybook", require: "view_component/storybook/engine"

# 1.0.9 fixed openssl issues on macOS https://github.com/eventmachine/eventmachine/issues/602
# While we don't require this gem directly, no dependents forced the upgrade to a version
# greater than 1.0.9, so we just required the latest available version here.
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,10 @@ GEM
get_process_mem (~> 0)
unicorn (>= 4, < 7)
uniform_notifier (1.14.1)
view_component (2.28.0)
activesupport (>= 5.0.0, < 7.0)
view_component_storybook (0.8.0)
view_component (>= 2.2)
warden (1.2.9)
rack (>= 2.0.9)
webdrivers (4.6.0)
Expand Down Expand Up @@ -722,6 +726,8 @@ DEPENDENCIES
uglifier (>= 1.0.3)
unicorn-rails
unicorn-worker-killer
view_component
view_component_storybook
web!
webdrivers
webmock
Expand Down
7 changes: 7 additions & 0 deletions app/components/distributor_title_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class DistributorTitleComponent < ViewComponent::Base
def initialize(name:)
@name = name
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
%h3= @name
7 changes: 7 additions & 0 deletions app/components/example_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class ExampleComponent < ViewComponent::Base
def initialize(title:)
@title = title
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
%h1 #{@title}
2 changes: 1 addition & 1 deletion app/controllers/spree/admin/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def invoice
end

def print
render InvoiceRenderer.new.args(@order)
render_with_wicked_pdf InvoiceRenderer.new.args(@order)
end

def print_ticket
Expand Down
2 changes: 1 addition & 1 deletion app/services/invoice_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def initialize(renderer = ApplicationController.new)

def render_to_string(order)
renderer.instance_variable_set(:@order, order)
renderer.render_to_string(args(order))
renderer.render_to_string_with_wicked_pdf(args(order))
end

def args(order)
Expand Down
3 changes: 1 addition & 2 deletions app/views/shopping_shared/_header.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
#distributor_title
- if distributor.logo?
%img.left{src: distributor.logo.url(:thumb)}
%h3
= distributor.name
= render DistributorTitleComponent.new(name: distributor.name)
%location= distributor.address.city

= yield :ordercycle_sidebar
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,7 @@ class Application < Rails::Application
config.active_support.escape_html_entities_in_json = true

config.active_job.queue_adapter = :delayed_job

config.generators.template_engine = :haml
end
end
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@
config.action_mailer.default_url_options = { host: "0.0.0.0:3000" }

config.log_level = :debug

config.view_component_storybook.stories_path = Rails.root.join("spec/components/stories")
end
14 changes: 14 additions & 0 deletions config/initializers/storybook.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Adjust headers to allow running Storybook in development.
# Uses iframes and doesn't play nicely with CORS checks

if Rails.env.development?
module PermissiveCORSHeaders
def self.before(response)
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Methods"] = "GET"
Copy link
Contributor

Choose a reason for hiding this comment

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

I think after the 5.2 upgrade is merged we could even replace this with something slightly nicer, as 5.2 introduces a new built-in DSL for modifying response headers 🎉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it is now the perfect time to do this, permissive CORS headers don't work anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See: d79ab55

end
end

ViewComponent::Storybook::StoriesController.before_action(PermissiveCORSHeaders)
end

8 changes: 8 additions & 0 deletions config/initializers/wicked_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@
#:layout => "pdf.html",
:exe_path => `bundle exec which wkhtmltopdf`.chomp
}

# A monkey-patch to remove WickedPdf's monkey-patch, as it clashes with ViewComponents.
class WickedPdf
module PdfHelper
remove_method(:render)
remove_method(:render_to_string)
end
end
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
"type": "git",
"url": "https://github.com/openfoodfoundation/openfoodnetwork"
},
"scripts": {
"storybook": "start-storybook"
},
"devDependencies": {
"@storybook/addon-controls": "^6.2.7",
"@storybook/addon-docs": "^6.2.7",
"@storybook/server": "^6.2.7",
"jasmine-core": "~2.4.1",
"karma": "~6.3.2",
"karma-chrome-launcher": "~3.1.0",
Expand Down
8 changes: 8 additions & 0 deletions spec/components/distributor_title_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "spec_helper"

describe "DistributorTitle tests", type: :component do
it "displays distributor title with its name" do
render_inline(DistributorTitleComponent.new(name: "Freddy's Farm Shop")) { }
expect(page).to have_selector "h3", text: "Freddy's Farm Shop"
end
end
8 changes: 8 additions & 0 deletions spec/components/example_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "spec_helper"

describe "ExampleComponent tests", type: :component do
it "displays the h1 with the given parameter" do
render_inline(ExampleComponent.new(title: "Hello")) { }
expect(page).to have_selector "h1", text: "Hello"
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class DistributorTitleComponentStories < ViewComponent::Storybook::Stories
story(:default) do
controls do
text(:name, "Freddy s Farm Shop")
end
end
end
13 changes: 13 additions & 0 deletions spec/components/stories/example_component_stories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ExampleComponentStories < ViewComponent::Storybook::Stories
story(:with_short_text) do
controls do
text(:title, 'OK')
end
end

story(:with_long_text) do
controls do
text(:title, 'This is a long text')
end
end
end
4 changes: 2 additions & 2 deletions spec/services/invoice_renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
it 'uses the invoice2 template' do
renderer = instance_double(ApplicationController)
expect(renderer)
.to receive(:render_to_string)
.to receive(:render_to_string_with_wicked_pdf)
.with(include(template: 'spree/admin/orders/invoice2'))

described_class.new(renderer).render_to_string(order)
Expand All @@ -38,7 +38,7 @@
it 'uses the invoice template' do
renderer = instance_double(ApplicationController)
expect(renderer)
.to receive(:render_to_string)
.to receive(:render_to_string_with_wicked_pdf)
.with(include(template: 'spree/admin/orders/invoice'))

described_class.new(renderer).render_to_string(order)
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
# Override setting in Spree engine: Spree::Core::MailSettings
ActionMailer::Base.default_url_options[:host] = 'test.host'

require "view_component/test_helpers"

RSpec.configure do |config|
# ## Mock Framework
#
Expand Down Expand Up @@ -232,6 +234,8 @@ def handle_webmock_error(exception)
# PerfTools::CpuProfiler.stop
# end
config.infer_spec_type_from_file_location!

config.include ViewComponent::TestHelpers, type: :component
end

FactoryBot.use_parent_strategy = false
Loading