-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UI controller concerns for fullstack rails (#7)
* add UI controller concerns for fullstack rails apps * add UI modules * fix offenses and specs
- Loading branch information
Showing
19 changed files
with
314 additions
and
46 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
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
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,68 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_support/concern" | ||
|
||
module Warped | ||
module Controllers | ||
module Filterable | ||
module Ui | ||
extend ActiveSupport::Concern | ||
include Filterable | ||
|
||
included do | ||
helper_method :filters, :filtered?, :current_filters, :filter_url_params, :filterable_by | ||
end | ||
|
||
def filter(...) | ||
@filtered = true | ||
|
||
super | ||
end | ||
|
||
def filtered? | ||
@filtered ||= false | ||
end | ||
|
||
def filter_url_params(**options) | ||
url_params = {} | ||
current_filters.each_with_object(url_params) do |filter, hsh| | ||
if filter[:value].is_a?(Array) | ||
filter[:value].each { |value| hsh["#{filter[:name]}[]"] = value } | ||
else | ||
hsh[filter[:name]] = filter[:value] | ||
end | ||
|
||
hsh["#{filter[:name]}.rel"] = filter[:relation] | ||
end | ||
|
||
url_params.tap { _1.merge!(options) } | ||
end | ||
|
||
def filters | ||
(filter_fields | mapped_filter_fields).map do |field| | ||
{ | ||
name: filter_mapped_name(field), | ||
value: filter_value(field), | ||
relation: filter_rel_value(field) | ||
} | ||
end | ||
end | ||
|
||
def current_filters | ||
(filter_fields | mapped_filter_fields).filter_map do |field| | ||
filter_value = filter_value(field) | ||
filter_rel_value = filter_rel_value(field) | ||
|
||
next if filter_value.blank? && %w[is_null is_not_null].exclude?(filter_rel_value) | ||
|
||
{ | ||
name: filter_mapped_name(field), | ||
value: filter_value, | ||
relation: filter_rel_value | ||
} | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_support/concern" | ||
|
||
module Warped | ||
module Controllers | ||
module Pageable | ||
module Ui | ||
extend ActiveSupport::Concern | ||
|
||
include Pageable | ||
|
||
included do | ||
helper_method :pagination, :paginated?, :paginate_url_params | ||
end | ||
|
||
def paginate_url_params(**options) | ||
url_params = { page:, per_page: } | ||
url_params.merge!(options) | ||
url_params | ||
end | ||
|
||
def pagination | ||
super.tap do |hsh| | ||
hsh[:series] = series(hsh[:page], hsh[:total_pages]) | ||
end | ||
end | ||
|
||
def paginate(...) | ||
@paginated = true | ||
|
||
super | ||
end | ||
|
||
def paginated? | ||
@paginated ||= false | ||
end | ||
|
||
private | ||
|
||
# @param page [Integer] | ||
# @param total_pages [Integer] | ||
# @return [Array] | ||
# the series method returns an array of page numbers and :gap symbols | ||
# the current page is a string, the others are integers | ||
# series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36] | ||
def series(page, total_pages) | ||
return ["1"] if total_pages == 1 | ||
|
||
if total_pages <= 9 | ||
(1..total_pages).to_a | ||
else | ||
[1, :gap, page - 2, page - 1, page.to_s, page + 1, page + 2, :gap, total_pages] | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_support/concern" | ||
|
||
module Warped | ||
module Controllers | ||
module Searchable | ||
module Ui | ||
extend ActiveSupport::Concern | ||
|
||
include Searchable | ||
|
||
included do | ||
helper_method :searched?, :search_url_params | ||
end | ||
|
||
def search(...) | ||
@searched = true | ||
|
||
super | ||
end | ||
|
||
def searched? | ||
@searched ||= false | ||
end | ||
|
||
def search_url_params(**options) | ||
url_params = { search_param => search_term } | ||
url_params.merge!(options) | ||
url_params | ||
end | ||
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
Oops, something went wrong.