Last active
October 4, 2020 02:06
-
-
Save julianrubisch/1be39bbd122e7a2a5525cf4611dccc10 to your computer and use it in GitHub Desktop.
StimulusReflex Concern-Like Architecture
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
class ArticlesController < ApplicationController | |
def index | |
@articles = Article.where(session[:articles][:param] => session[:articles][:value]) | |
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
class FilterableReflex < ApplicationReflex | |
def filter | |
resource, param = element.dataset.to_h.fetch_values(:resource, :param) | |
resource_key = resource.pluralize.underscore.to_sym | |
session[resource_key][:param] = param | |
session[resource_key][:value] = element.value | |
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
<%= label_tag "category" %> | |
<%= select_tag "category", | |
options_from_collection_for_select(Category.all, :id, :name) | |
data: { action: "change->filterable#filter", | |
resource: 'Article', | |
param: 'category', | |
reflex_root: "#articles" } %> | |
<div id="articles"> | |
<%= render @articles %> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment