-
Notifications
You must be signed in to change notification settings - Fork 6
feat: configurable distance display unit #411
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module RedmineGtt | ||
| # Converts between meters and the display unit configured in the plugin | ||
| # settings (#10). Meters are the internal unit everywhere: storage, SQL, | ||
| # the filter wire format and the REST API all stay metric; only what the | ||
| # user sees and types is converted. | ||
| module DistanceUnit | ||
|
|
||
| METERS_PER_UNIT = { | ||
| 'm' => 1.0, | ||
| 'km' => 1000.0, | ||
| 'ft' => 0.3048, | ||
| 'mi' => 1609.344, | ||
| 'nm' => 1852.0 | ||
| }.freeze | ||
|
|
||
| DEFAULT = 'm' | ||
|
|
||
| # Units the REST API may use. Reserved for future flexibility: the API | ||
| # contract is meters and only meters for now, but the setting exists so | ||
| # a later version can widen this list without a settings migration. | ||
| API_UNITS = %w(m).freeze | ||
|
|
||
| # The configured display unit, falling back to meters for anything | ||
| # unknown (empty setting, typo from a manually edited setting hash). | ||
| def self.current | ||
| unit = Setting.plugin_redmine_gtt['distance_unit'].to_s | ||
| METERS_PER_UNIT.key?(unit) ? unit : DEFAULT | ||
| end | ||
|
|
||
| # The unit used by the REST API. Clamped to meters regardless of the | ||
| # stored value while API_UNITS has a single entry. | ||
| def self.api_unit | ||
| unit = Setting.plugin_redmine_gtt['api_distance_unit'].to_s | ||
| API_UNITS.include?(unit) ? unit : DEFAULT | ||
| end | ||
|
|
||
| def self.from_meters(meters, unit = current) | ||
| meters.to_f / METERS_PER_UNIT.fetch(unit) | ||
| end | ||
|
|
||
| def self.to_meters(value, unit = current) | ||
| value.to_f * METERS_PER_UNIT.fetch(unit) | ||
| end | ||
|
dkastl marked this conversation as resolved.
|
||
|
|
||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| module RedmineGtt | ||
| module Patches | ||
|
|
||
| # Renders the distance column in the configured display unit (#10). | ||
| # The underlying value stays meters (it is what the query selects and | ||
| # what the REST API returns); only the rendered list/CSV cell converts. | ||
| module QueriesHelperPatch | ||
|
|
||
| def self.apply | ||
| QueriesHelper.prepend self unless QueriesHelper < self | ||
| end | ||
|
|
||
| def column_value(column, item, value) | ||
| if column.name == :distance && value.present? | ||
| gtt_format_distance(value) | ||
| else | ||
| super | ||
| end | ||
| end | ||
|
|
||
| def csv_value(column, object, value) | ||
| if column.name == :distance && value.present? | ||
| # mirror core's CSV float formatting incl. the decimal separator | ||
| gtt_format_distance(value).gsub('.', l(:general_csv_decimal_separator)) | ||
| else | ||
| super | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def gtt_format_distance(meters) | ||
| sprintf('%.2f', RedmineGtt::DistanceUnit.from_meters(meters)) | ||
| end | ||
|
|
||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| require_relative '../test_helper' | ||
|
|
||
| class DistanceUnitTest < GttTest | ||
|
|
||
| teardown do | ||
| Setting.plugin_redmine_gtt = Setting.plugin_redmine_gtt.merge( | ||
| 'distance_unit' => 'm', 'api_distance_unit' => 'm' | ||
| ) | ||
| end | ||
|
|
||
| test 'defaults to meters' do | ||
| assert_equal 'm', RedmineGtt::DistanceUnit.current | ||
| assert_equal 'm', RedmineGtt::DistanceUnit.api_unit | ||
| end | ||
|
|
||
| test 'falls back to meters for unknown units' do | ||
| Setting.plugin_redmine_gtt = Setting.plugin_redmine_gtt.merge( | ||
| 'distance_unit' => 'parsec' | ||
| ) | ||
| assert_equal 'm', RedmineGtt::DistanceUnit.current | ||
| end | ||
|
|
||
| test 'reads the configured display unit' do | ||
| Setting.plugin_redmine_gtt = Setting.plugin_redmine_gtt.merge( | ||
| 'distance_unit' => 'km' | ||
| ) | ||
| assert_equal 'km', RedmineGtt::DistanceUnit.current | ||
| end | ||
|
|
||
| test 'api unit is clamped to meters regardless of the stored value' do | ||
| Setting.plugin_redmine_gtt = Setting.plugin_redmine_gtt.merge( | ||
| 'api_distance_unit' => 'km' | ||
| ) | ||
| assert_equal 'm', RedmineGtt::DistanceUnit.api_unit | ||
| end | ||
|
|
||
| test 'converts from meters into the given unit' do | ||
| assert_equal 1.5, RedmineGtt::DistanceUnit.from_meters(1500, 'km') | ||
| assert_in_delta 0.932, RedmineGtt::DistanceUnit.from_meters(1500, 'mi'), 0.001 | ||
| assert_in_delta 4921.26, RedmineGtt::DistanceUnit.from_meters(1500, 'ft'), 0.01 | ||
| assert_in_delta 0.81, RedmineGtt::DistanceUnit.from_meters(1500, 'nm'), 0.001 | ||
| assert_equal 1500.0, RedmineGtt::DistanceUnit.from_meters(1500, 'm') | ||
| end | ||
|
|
||
| test 'converts a unit value into meters' do | ||
| assert_equal 1500.0, RedmineGtt::DistanceUnit.to_meters(1.5, 'km') | ||
| assert_in_delta 1609.344, RedmineGtt::DistanceUnit.to_meters(1, 'mi'), 0.001 | ||
| assert_equal 25.0, RedmineGtt::DistanceUnit.to_meters(25, 'm') | ||
| end | ||
|
|
||
| test 'conversions honor the configured unit by default' do | ||
| Setting.plugin_redmine_gtt = Setting.plugin_redmine_gtt.merge( | ||
| 'distance_unit' => 'km' | ||
| ) | ||
| assert_equal 2.0, RedmineGtt::DistanceUnit.from_meters(2000) | ||
| assert_equal 2000.0, RedmineGtt::DistanceUnit.to_meters(2) | ||
| end | ||
|
|
||
| test 'distance column caption carries the unit' do | ||
| Setting.plugin_redmine_gtt = Setting.plugin_redmine_gtt.merge( | ||
| 'distance_unit' => 'mi' | ||
| ) | ||
| column = IssueQuery.new.available_columns.detect { |c| c.name == :distance } | ||
| assert column, 'distance column should be available on global queries' | ||
| assert_match(/\(mi\)\z/, column.caption) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| require_relative '../test_helper' | ||
|
|
||
| class QueriesHelperPatchTest < Redmine::HelperTest | ||
| include QueriesHelper | ||
| include ApplicationHelper | ||
| include ERB::Util | ||
|
|
||
| teardown do | ||
| Setting.plugin_redmine_gtt = Setting.plugin_redmine_gtt.merge( | ||
| 'distance_unit' => 'm' | ||
| ) | ||
| end | ||
|
|
||
| def distance_column | ||
| IssueQuery.new.available_columns.detect { |c| c.name == :distance } | ||
| end | ||
|
|
||
| test 'renders the distance column in the configured unit' do | ||
| Setting.plugin_redmine_gtt = Setting.plugin_redmine_gtt.merge( | ||
| 'distance_unit' => 'km' | ||
| ) | ||
| assert_equal '1.50', column_value(distance_column, nil, 1500.0) | ||
| end | ||
|
|
||
| test 'renders meters by default' do | ||
| assert_equal '1500.00', column_value(distance_column, nil, 1500.0) | ||
| end | ||
|
|
||
| test 'renders the csv value with the locale decimal separator' do | ||
| Setting.plugin_redmine_gtt = Setting.plugin_redmine_gtt.merge( | ||
| 'distance_unit' => 'km' | ||
| ) | ||
| assert_equal '1.50', csv_value(distance_column, nil, 1500.0) | ||
| end | ||
|
|
||
| test 'leaves blank distance values to core' do | ||
| assert_equal '', column_value(distance_column, nil, nil).to_s | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.