-
Notifications
You must be signed in to change notification settings - Fork 19
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
Fix generate_html method in googlecharts.rb #84
Conversation
@@ -70,11 +71,13 @@ def show_in_iruby(plot) | |||
end | |||
|
|||
def generate_html(plot) | |||
# TODO: modify code | |||
path = File.expand_path('../../templates/googlecharts/chart_div.erb', __FILE__) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think instead of this chart_div.erb
file, you can use your static_html.erb
file. Since we have initial_script
and chart_script
(you are using it as chart_div
).
path = File.expand_path('../../templates/googlecharts/chart_div.erb', __FILE__) | ||
template = File.read(path) | ||
template_div = File.read(path) | ||
chart_script, id = generate_body(plot) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
id is randomly generated, so no need to get id
, isn't it?
<%= initial_script %> | ||
</head> | ||
<body><%= chart_div %></body> | ||
</html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<enter>
here.
Thanks for PR. But we must open an issue first (rather than PR), if you find any bug or problem in code, so that all issue related discussion goes there, also it will clarify the problem and approach to solve it. Because fixing a bug without any discussion may cause to waste of time. Please look into this comment. |
Please write testcases for all related methods. It is really needed. |
Sure, I will do it. |
I was trying to test the method
However, the script to test contains the random id generated, so it is difficult to match the expected output. I was thinking of using regex but IMO this is not a very good idea. What should I do? |
You can match specific tags and Javascript code to test, it is generating the correct Javascript. Although you can pass custom id in plot#to_html method. You may need to change in few upper level methods to pass custom id in export_html_file method. |
2e6b5cf
to
2e2d1fd
Compare
As you have suggested I have passed the custom id in the to_html method by changing few upper-level methods. IMO matching specific tags can be a bug-prone task. Should I need to change anything? |
spec/adapters/googlecharts_spec.rb
Outdated
allow(File).to receive(:write) | ||
area_chart_chart. | ||
export_html_file('./plot.html','f75b3117-7cb0-4451-add4-e0006cb10876') | ||
path = File.expand_path('../../plot.html', __dir__) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that it is good to test whole html code here. As I said
we must test all related methods, so it is better to start test from the lower to upper level methods. Test for initial_script
, chart_script
i.e. #to_html
, #generate_body
,#generate_html
methods .
I think we must test for specific keys (in this case area chart, options, data, etc in javascript code) . Is it good to test whole file at once ? Ping @zverok @v0dro
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I was busy with the proposal I was unable to work on this.
I will test for the methods separately but I still think that testing the specific keys would be bug-prone. For instance, to_html method will return the rendered html in string format in ruby. So, extracting the chart, options and data in javascript code might be a diificult task.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because for extracting the chart
, options
and data
from the JavaScript code, I think I will have to search for this text in the string returned. Is there any way to directly get those keys from the JS code without implementing a search?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe pattern matching will be useful. You can explore how it is tested in google_visualr
, lazy_high_charts
or other gems like formtastic
etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add example(s) in spec/dummy_iruby (and share the link) and in rails, nanoc, sinatra app.
When it will be merged we need to have one example in our demo rails app : https://github.com/Shekharrajak/daru_examples_io_view_rails
expect(js).to match( | ||
/data_table.addRow\(\[\{v: \"2013\"\}\]\);/i) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expecting testcases for show_script
as well.
expect(js).to match( | ||
/data_table.addRow\(\[\{v: \"2013\"\}\]\);/i) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expecting similar test cases for init_script
, export_html_file
, try for init_iruby
. Also please test with few other chart types as well.
@@ -0,0 +1,7 @@ | |||
<html lang='en'> | |||
<head> | |||
<title>Chart</title> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if it is a google datatable ? I think we can add more information in title (id, chart type). We can extend it and display with table and chart both in html table ( mark as TODO. It must not be in this same PR).
<title>Chart</title> | ||
<%= initial_script %> | ||
</head> | ||
<body><%= chart_script %></body> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer
<body>
<%= chart_script %>
</body>
It already comes in bunch of js code. So alignment is important.
What kind of examples do I need to add in dummy_iruby as I think it already has enough examples of google charts? Should I indicate the use of generate_html method in the examples? |
20fdb33
to
e73c088
Compare
@Shekharrajak can you please review this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For id
part I am expecting some other way. Let me know if you have some idea otherwise it will be merged.
plot.to_html[0] | ||
end | ||
|
||
def generate_id(plot, id=nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not satisfied with this kind of approach. Anyways I don't understand why there is id
parameter and function name generate_id
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As the generated id
is random, I have used the id
parameter for the testing purpose here
Random |
Consider #84 (comment) . |
initial_script = init_script | ||
id = plot.id_chart |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's html_id
not id_chart
(or some meaningful).
@@ -22,7 +22,7 @@ | |||
|
|||
describe "#to_html" do | |||
it "generates valid JS of the Area Chart" do | |||
js, id = area_chart_chart.chart.to_html("id") | |||
js = area_chart_chart.chart.to_html("id") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check for "id" in div tag.
@@ -14,6 +14,7 @@ def self.init_script( | |||
end | |||
|
|||
module Display | |||
attr_accessor :html_id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We must write about html_id
. It will hold a value only when to_html
method is invoked (i.e. html code of the chart is generated).
Ufff we are iterating too many time. You must check these things for your PR :
- Code quality (Clear, understandable, Extensible, exception handling, ..)
- Specs for the feature or your each line of code.
- Documentation (It is always better to have it. There is lack of documentations already for internal methods). Please follow yard.
Looks good to go. Thanks! |
Fix for the issue #85
This PR fixes the bug for the generate_html method in googlecharts.rb which I found while going through the code.
Wrong argument
plots
was passed as the argument in the generate_body method here: https://github.com/SciRuby/daru-view/blob/master/lib/daru/view/adapters/googlecharts.rb#L77There was no
id
defined in the generate_html method which was further used while rendering template here: https://github.com/SciRuby/daru-view/blob/master/lib/daru/view/adapters/googlecharts.rb#L78Also, the HTML of the chart contained only the div part, so I created another template to include the
initial_script
also.