-
-
Notifications
You must be signed in to change notification settings - Fork 108
add set_order function #267
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
Conversation
dojo['order']に応じて値をsetするサブルーチンを定義しました。 環境変数 SET_JSONにGoogleAPIのjsonファイルを、SPREAD_SHEET_KEYにスプレッドシートのURLを設定すると使用可能です。 - 見つからなかった場合の処理 - デプロイ時にjsonファイルを設定することが可能なのか - `dojos.rake` では最初に `order` に応じてsortしている為適応順序の問題
lib/tasks/dojos.rake
Outdated
|
||
return name if name =~ /^[0-9]+$/ | ||
|
||
conf = File.expand_path(ENV['SET_JSON']) |
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.
ENV['SET_JSON']
がnilの場合、結果としてエラーになるのは一緒なのですが、
エラーの内容が全然違うものになるのでENV.fetch('SET_JSON')
の方が良いかなーと思いました。
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.
あと、SET_JSONって名前、もう少し直感的にどういうJSONファイルかが分かる名前だと良いなとおもいます。
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.
ありがとうございます。 ENV.fetch
だと例外が発生するんですね。
命名規則気を配ってきいます
lib/tasks/dojos.rake
Outdated
|
||
conf = File.expand_path(ENV['SET_JSON']) | ||
session = GoogleDrive:: Session.from_config(conf) | ||
spred_sheet = session.spreadsheet_by_key(ENV['SPREAD_SHEET_KEY']).worksheets[0] |
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.
ditto
こっちの PRではGoogleスプレッドシートにアクセスを行うように書いたのですがissue #228 の通り またこれは質問なんですが、例外を発生させる時の良い感じの方法を教えていただきたいです。 |
また気づいたのですが名前は |
例外を発生させるのはraiseですねー。 |
resolve coderdojo-japan#228 でGoogleスプレッドシートにアクセスを行うとjsonファイルを別途生成するなどの必要性が出てきた。 デプロイ環境でスプレッドシートにアクセス出来るかが怪しい為、スプレッドシートをローカルに落とし、実行出来るようにした. > また気づいたのですが名前は 鳥取 (中国) といった target (地域) の表記となるので正規表現を追加します ここから、名前を正規表現で都市のみ抜き出して前文検索でorderを引くように設定した。
> こっちの PRではGoogleスプレッドシートにアクセスを行うように書いたのですがissue coderdojo-japan#228 の通り一旦ローカルにCSVを落とし、そこから検索を行うという処理を行おうと思います。 現在スプレッドシートに上がっている全国地方公共団体コードの1ページを抜き 取りcsv化した。 文字コードの関係上 `nkf -Lu --overwrite local_public_organization.csv`を 実行し、扱いやすいようにheaderを修正した。
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.
12行目dojos.sort_by!{ |hash| hash['order'] }
の部分で order
にもとづいてsort
しているのですが、この処理を代替えすることが可能か検討しています。( order
の値が決定しないとソートが不可能になっているため)
> 12行目dojos.sort_by!{ |hash| hash['order'] } の部分で order にもとづい てsort しているのですが、この処理を代替えすることが可能か検討しています 。( order の値が決定しないとソートが不可能になっているため) 今回のソートは表示順ではなく保存の順序を決定しているのみであるので、削除 しても動作上問題がないと考えられる為削除した。
lib/tasks/dojos.rake
Outdated
|
||
return pre_municipality if pre_municipality =~ /^[0-9]+$/ | ||
|
||
if /(?<city>.+)\s\(.+\)/ =~ pre_municipality |
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.
municipality って言葉はほとんど聞かないので、town
とかcity
とかでどうですかね? 🤔
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.
もしくは region
とか。
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.
ありがとうございます! city
にrenameします
lib/tasks/dojos.rake
Outdated
return pre_municipality if pre_municipality =~ /^[0-9]+$/ | ||
|
||
if /(?<city>.+)\s\(.+\)/ =~ pre_municipality | ||
table = CSV.table(Rails.root.join('db','local_public_organization.csv')) |
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.
local_public_organization.csv
このファイル名だと分かりづらいですね 😓 city_code.csv
とか city_to_code.csv
とかどうでしょう? 🤔
> municipality って言葉はほとんど聞かないので、city でどうですかね? 🤔 > このファイル名だと分かりづらいですね 😓 city_code.csv とかcity_to_code.csv とかどうでしょう? 🤔 命名が分かりづらかった為,レビューを反映しました。 csvファイルの該当カラムも同時に命名を変更しています
レビューを反映して、変数名を修正しました! |
良さそう! @nalabjp さんのレビューを待ってからマージしましょう ;) |
lib/tasks/dojos.rake
Outdated
@@ -18,20 +19,20 @@ namespace :dojos do | |||
dojo.delete 'updatedAt' # This is managed by database | |||
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.
不要な改行ですかね?
lib/tasks/dojos.rake
Outdated
|
||
dojos.each do |dojo| | ||
d = Dojo.find_or_initialize_by(id: dojo['id']) | ||
|
||
d.name = dojo['name'] | ||
d.email = '' | ||
d.order = dojo['order'] | ||
d.order = set_order( dojo['order'] ? dojo['order']: dojo['name']) |
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.
dojo['order']があればdojo['order']を、なければdojo['name']をset_orderの引数に使用する。
その上で、L52の実装により、
引数に渡ってきた値が数値のみの場合はそのまま返却する、ということであれば、
dojo['order'] || set_order(dojo['name'])
とするほうが意図が明確でメソッドコールもなくなるので良さそうだなと思いました。
あわせてL52の実装については不要になるかなと思います。
lib/tasks/dojos.rake
Outdated
@@ -44,6 +45,21 @@ namespace :dojos do | |||
end | |||
end | |||
|
|||
# search order number for google spred sheets | |||
# 'yamlファイルのnameからorderの値を生成します' | |||
def set_order(pre_city) |
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.
setしているわけではないのでsearch_order or search_order_numberの方が直感的かなと思いましたがどうでしょうか?
lib/tasks/dojos.rake
Outdated
# 'yamlファイルのnameからorderの値を生成します' | ||
def set_order(pre_city) | ||
|
||
return pre_city if pre_city =~ /^[0-9]+$/ |
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.
上でコメントしたとおりdojo['name']しか渡ってこないなら不要ですかね?
lib/tasks/dojos.rake
Outdated
if /(?<city>.+)\s\(.+\)/ =~ pre_city | ||
table = CSV.table(Rails.root.join('db','city_code.csv')) | ||
row = table.find{ |r| r[:city].to_s.start_with?(city)} | ||
row ? row[:order] : raise("Can't searched order by #{pre_city}") |
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.
僕も英語が得意なわけではないので自信ないんですが、
「検索できなかった」と過去形にしたいなら「Couldn't search」の方だったりしますかね?
ちょっと違和感があったのでgoogle翻訳に入れてみたら「もしかして Can't search 〜」と言われたので😅
よくある語彙としては「Not found」かなーと思うのでそれでも良いかなと思いました😌
nalabjpさんのレビューを反映しました > dojo['order'] || set_order(dojo['name'])とするほうが意図が明確でメソッドコールもなくなるので良さそうだなと思いました。 > あわせてL52の実装については不要になるかなと思います。 確かにメソッドコールのコストが掛からなくなる為修正しました メソッド内部の判定部分も不要になるため削除しました またsearch_order_numberに名前を修正しています。
@nalabjp さんのレビューを反映しました! |
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.
LGTM 👀 👍
good works 🎉
ありがとうございます! お二人かapproveがきていたのでmergeします |
Nice work!! 👍✨ |
今回のマージで |
Fix coderdojo-japan#268 > 今回のマージでorder 周りの設定手順が変わったと思うので、docs/how-to-add-dojo.md のドキュメントにも反映しておくと良いかも > cf. coderdojo-japan#267 `order`はnameから取っている為、市町村と同じDojo名だと省略可能な旨を追記 した
coderdojo-japan#267 で`order` を自動追加出来るように設定していたが yamlファイルを出力するタイミングで,orderを出力するようにした
目的
#228 である通り,orderの値をGoogleスプレッドシートを参照せず設定出来るようにする
行ったこと
dojo['order']に応じて値をsetするサブルーチンを定義しました- 当初はtaskとして切り出す事を考えていましたが、1手間かかることには変わりないので内部にいれました
環境変数SET_JSON
にGoogleAPIのjsonファイル環境変数SPREAD_SHEET_KEY
にスプレッドシートのURLを設定すると使用可能です。CSVファイルとしてスプレッドシートをローカルに落とす
CSVファイルから
dojo['name']
に応じて都市名のorder
を返す該当しない、及び
都市 (地域)
の記法出ない場合は例外を返すすでに
order
が決定している場合は何もしないテストコード
困っている事
見つからなかった場合の処理デプロイ時にjsonファイルを設定することが可能なのかdojos.rake
では最初にorder
に応じてsortしている為に、適応順序の問題