Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the flag calculation into a model
To make the webui code we did for repositories controller also useful for the API to avoid having 2 ways for the same
- Loading branch information
Showing
10 changed files
with
67 additions
and
98 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,41 +1,6 @@ | ||
module Webui::RepositoryHelper | ||
def effective_flag(flags, repository, architecture) | ||
dig_flag(:effective_flag_for, flags, repository, architecture) | ||
end | ||
|
||
def default_flag(flags, repository, architecture) | ||
dig_flag(:default_flag_for, flags, repository, architecture) | ||
end | ||
|
||
def flag_set_by_user?(flags, repository, architecture) | ||
flags[:object].dig(repository, architecture).present? | ||
end | ||
|
||
def html_id_for_flag(flag_type, repository, architecture) | ||
# repository and architecture can be nil | ||
valid_xml_id("flag-#{flag_type}-#{repository}-#{architecture}") | ||
end | ||
|
||
# TODO: This should be private | ||
def dig_flag(callback_function, flags, repository, architecture) | ||
flag = send(callback_function, flags[:object], repository, architecture) | ||
flag ||= send(callback_function, flags[:project], repository, architecture) if flags[:project] | ||
flag || flags[:default] | ||
end | ||
|
||
def effective_flag_for(flags, repository, architecture) | ||
flags.dig(repository, architecture) || flags.dig(repository, nil) || flags.dig(nil, architecture) || flags.dig(nil, nil) | ||
end | ||
|
||
# It finds out how the table would look like if the flag was not set. | ||
# In case of specific flags this means lookup the rest, for flags that are | ||
# only specifying one direction, we need to look at the overall state | ||
# (default state is handled in the function above). | ||
def default_flag_for(flags, repository, architecture) | ||
if repository && architecture | ||
flags.dig(repository, nil) || flags.dig(nil, architecture) || flags.dig(nil, nil) | ||
elsif architecture || repository | ||
flags.dig(nil, nil) | ||
end | ||
end | ||
end |
This file contains 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 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 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,55 @@ | ||
class Flag::SpecifiedFlags | ||
def initialize(prj_or_pkg, flag_type) | ||
@flags = {} | ||
@flags[:object] = specified_flags(prj_or_pkg, flag_type) | ||
@flags[:default] = Flag.new(flag: flag_type, status: FlagHelper.default_for(flag_type)) | ||
@flags[:project] = specified_flags(prj_or_pkg.project, flag_type) if prj_or_pkg.is_a?(Package) | ||
end | ||
|
||
def effective_flag(repository, architecture) | ||
dig_flag(:effective_flag_for, repository, architecture) | ||
end | ||
|
||
def default_flag(repository, architecture) | ||
dig_flag(:default_flag_for, repository, architecture) | ||
end | ||
|
||
def set_by_user?(repository, architecture) | ||
@flags[:object].dig(repository, architecture).present? | ||
end | ||
|
||
private | ||
|
||
def dig_flag(callback_function, repository, architecture) | ||
flag = send(callback_function, @flags[:object], repository, architecture) | ||
flag ||= send(callback_function, @flags[:project], repository, architecture) if @flags[:project] | ||
flag || @flags[:default] | ||
end | ||
|
||
def effective_flag_for(flags, repository, architecture) | ||
flags.dig(repository, architecture) || flags.dig(repository, nil) || flags.dig(nil, architecture) || flags.dig(nil, nil) | ||
end | ||
|
||
# It finds out how the table would look like if the flag was not set. | ||
# In case of specific flags this means lookup the rest, for flags that are | ||
# only specifying one direction, we need to look at the overall state | ||
# (default state is handled in the function above). | ||
def default_flag_for(flags, repository, architecture) | ||
if repository && architecture | ||
flags.dig(repository, nil) || flags.dig(nil, architecture) || flags.dig(nil, nil) | ||
elsif architecture || repository | ||
flags.dig(nil, nil) | ||
end | ||
end | ||
|
||
def specified_flags(prj_or_pkg, flag_type) | ||
all_flags = prj_or_pkg.flags.where(flag: flag_type).group_by(&:repo) | ||
|
||
all_flags.each do |repo, flag_array| | ||
all_flags[repo] = {} | ||
flag_array.each do |flag| | ||
all_flags[repo][flag.architecture.try(&:name)] = flag | ||
end | ||
end | ||
end | ||
end |
This file contains 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 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 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 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
4 changes: 2 additions & 2 deletions
4
src/api/app/views/webui2/shared/_repositories_flag_table_column.html.haml
This file contains 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