Skip to content
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

Feature/cclow sector filter #279

Merged
merged 3 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def index
types_filter_options: legislation_types_options,
instruments_filter_options: instruments_options,
governances_filter_options: governances_options,
sectors_options: sectors_options('Legislation'),
legislations: CCLOW::LegislationDecorator.decorate_collection(@legislations.first(10)),
count: @legislations.count
}, prerender: false
Expand Down
1 change: 1 addition & 0 deletions app/controllers/cclow/litigation_cases_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def index
litigation_side_a_party_type_options: litigation_side_a_party_type_options,
litigation_side_b_party_type_options: litigation_side_b_party_type_options,
litigation_side_c_party_type_options: litigation_side_c_party_type_options,
sectors_options: sectors_options('Litigation'),
litigations: CCLOW::LitigationDecorator.decorate_collection(@litigations.first(10)),
count: @litigations.size
}, prerender: false
Expand Down
8 changes: 7 additions & 1 deletion app/controllers/concerns/cclow/filter_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,18 @@ def litigation_jurisdictions_options
[{field_name: 'jurisdiction', options: jurisdiction_types.sort_by { |h| h[:label] }}]
end

def sectors_options(class_name)
name = class_name.downcase.pluralize
sectors = LawsSector.joins(name.to_sym).distinct.map { |j| {value: j.id, label: j.name} }
[{field_name: 'law_sector', options: sectors.sort_by { |h| h[:label] }}]
end

def filter_params
params.permit(:q, :from_date,
:to_date, :recent, :ids, region: [], geography: [], jurisdiction: [],
status: [], type: [], keywords: [], responses: [],
frameworks: [], natural_hazards: [], party_type: [],
side_a: [], side_b: [], side_c: [],
side_a: [], side_b: [], side_c: [], law_sector: [],
a_party_type: [], b_party_type: [], c_party_type: [],
instrument: [], governance: [])
end
Expand Down
31 changes: 24 additions & 7 deletions app/javascript/components/pages/cclow/LegislationAndPolicies.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class LegislationAndPolicies extends Component {
activeTypesFilter: {},
activeInstrumentsFilter: {},
activeNaturalHazardsFilter: {},
activeGovernancesFilter: {}
activeGovernancesFilter: {},
activeSectorsFilter: {}
};

this.geoFilter = React.createRef();
Expand All @@ -43,6 +44,7 @@ class LegislationAndPolicies extends Component {
this.instrumentsFilter = React.createRef();
this.naturalHazardsFilter = React.createRef();
this.governancesFilter = React.createRef();
this.sectorsFilter = React.createRef();
}

getQueryString(extraParams = {}) {
Expand All @@ -55,7 +57,8 @@ class LegislationAndPolicies extends Component {
activeInstrumentsFilter,
activeNaturalHazardsFilter,
activeGovernancesFilter,
activeTimeRangeFilter
activeTimeRangeFilter,
activeSectorsFilter
} = this.state;

const params = {
Expand All @@ -69,6 +72,7 @@ class LegislationAndPolicies extends Component {
...activeNaturalHazardsFilter,
...activeGovernancesFilter,
...activeTimeRangeFilter,
...activeSectorsFilter,
...extraParams
};

Expand Down Expand Up @@ -127,7 +131,8 @@ class LegislationAndPolicies extends Component {
frameworks_filter_options: frameworksFilterOptions,
instruments_filter_options: instrumentsFilterOptions,
natural_hazards_filter_options: naturalHazardsFilterOptions,
governances_filter_options: governancesFilterOptions
governances_filter_options: governancesFilterOptions,
sectors_options: sectorsOptions
} = this.props;

return (
Expand Down Expand Up @@ -179,6 +184,12 @@ class LegislationAndPolicies extends Component {
params={governancesFilterOptions}
onChange={(event) => this.filterList('activeGovernancesFilter', event)}
/>
<SearchFilter
ref={this.sectorsFilter}
filterName="Sectors"
params={sectorsOptions}
onChange={(event) => this.filterList('activeSectorsFilter', event)}
/>
<button
type="button"
onClick={() => this.setState({isMoreSearchOptionsVisible: false})}
Expand All @@ -202,7 +213,8 @@ class LegislationAndPolicies extends Component {
activeTypesFilter,
activeInstrumentsFilter,
activeNaturalHazardsFilter,
activeGovernancesFilter
activeGovernancesFilter,
activeSectorsFilter
} = this.state;
const {
geo_filter_options: geoFilterOptions,
Expand All @@ -212,7 +224,8 @@ class LegislationAndPolicies extends Component {
types_filter_options: typesFilterOptions,
instruments_filter_options: instrumentsFilterOptions,
natural_hazards_filter_options: naturalHazardsFilterOptions,
governances_filter_options: governancesFilterOptions
governances_filter_options: governancesFilterOptions,
sectors_options: sectorsOptions
} = this.props;
if (Object.keys(activeGeoFilter).length === 0
&& Object.keys(activeKeywordsFilter).length === 0
Expand All @@ -221,6 +234,7 @@ class LegislationAndPolicies extends Component {
&& Object.keys(activeInstrumentsFilter).length === 0
&& Object.keys(activeNaturalHazardsFilter).length === 0
&& Object.keys(activeGovernancesFilter).length === 0
&& Object.keys(activeSectorsFilter).length === 0
&& Object.keys(activeTimeRangeFilter).length === 0) return null;
return (
<div className="filter-tags">
Expand All @@ -229,6 +243,7 @@ class LegislationAndPolicies extends Component {
{this.renderTagsGroup(activeResponsesFilter, responsesFilterOptions, 'responsesFilter')}
{this.renderTagsGroup(activeFrameworksFilter, frameworksFilterOptions, 'frameworksFilter')}
{this.renderTagsGroup(activeTypesFilter, typesFilterOptions, 'typesFilter')}
{this.renderTagsGroup(activeSectorsFilter, sectorsOptions, 'sectorsFilter')}
{this.renderTagsGroup(activeInstrumentsFilter, instrumentsFilterOptions, 'instrumentsFilter')}
{this.renderTagsGroup(activeNaturalHazardsFilter, naturalHazardsFilterOptions, 'naturalHazardsFilter')}
{this.renderTagsGroup(activeGovernancesFilter, governancesFilterOptions, 'governancesFilter')}
Expand Down Expand Up @@ -375,7 +390,8 @@ LegislationAndPolicies.defaultProps = {
types_filter_options: [],
instruments_filter_options: [],
natural_hazards_filter_options: [],
governances_filter_options: []
governances_filter_options: [],
sectors_options: []
};

LegislationAndPolicies.propTypes = {
Expand All @@ -388,7 +404,8 @@ LegislationAndPolicies.propTypes = {
types_filter_options: PropTypes.array,
instruments_filter_options: PropTypes.array,
natural_hazards_filter_options: PropTypes.array,
governances_filter_options: PropTypes.array
governances_filter_options: PropTypes.array,
sectors_options: PropTypes.array
};

export default LegislationAndPolicies;
45 changes: 31 additions & 14 deletions app/javascript/components/pages/cclow/LitigationCases.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ class LitigationCases extends Component {
activeSideAPartyTypesFilter: {},
activeSideBPartyTypesFilter: {},
activeSideCPartyTypesFilter: {},
activeJurisdictionsFilter: {}
activeJurisdictionsFilter: {},
activeSectorsFilter: {}
};

this.geoFilter = React.createRef();
this.keywordsFilter = React.createRef();
this.responsesFilter = React.createRef();
this.timeRangeFilter = React.createRef();
this.sectorsFilter = React.createRef();
this.statusFilter = React.createRef();
this.sideAFilter = React.createRef();
this.sideBFilter = React.createRef();
Expand All @@ -62,6 +64,7 @@ class LitigationCases extends Component {
activeSideAFilter,
activeSideBFilter,
activeSideCFilter,
activeSectorsFilter,
activeJurisdictionsFilter,
activeSideAPartyTypesFilter,
activeSideBPartyTypesFilter,
Expand All @@ -77,6 +80,7 @@ class LitigationCases extends Component {
...activeSideAFilter,
...activeSideBFilter,
...activeSideCFilter,
...activeSectorsFilter,
...activePartyTypesFilter,
...activeJurisdictionsFilter,
...activeSideAPartyTypesFilter,
Expand Down Expand Up @@ -126,6 +130,7 @@ class LitigationCases extends Component {
activeSideAFilter,
activeSideBFilter,
activeSideCFilter,
activeSectorsFilter,
activePartyTypesFilter,
activeJurisdictionsFilter,
activeSideAPartyTypesFilter,
Expand All @@ -140,6 +145,7 @@ class LitigationCases extends Component {
litigation_side_a_names_options: litigationSideAOptions,
litigation_side_b_names_options: litigationSideBOptions,
litigation_side_c_names_options: litigationSideCOptions,
sectors_options: sectorsOptions,
litigation_party_types_options: litigationPartyTypesOptions,
litigation_jurisdictions_options: litigationJurisdictionsOptions,
litigation_side_a_party_type_options: litigationSideAPartyTypeOptions,
Expand All @@ -154,6 +160,7 @@ class LitigationCases extends Component {
&& !Object.keys(activeSideAFilter).length
&& !Object.keys(activeSideBFilter).length
&& !Object.keys(activeSideCFilter).length
&& !Object.keys(activeSectorsFilter).length
&& !Object.keys(activeSideAPartyTypesFilter).length
&& !Object.keys(activeSideBPartyTypesFilter).length
&& !Object.keys(activeSideCPartyTypesFilter).length
Expand All @@ -167,6 +174,7 @@ class LitigationCases extends Component {
{this.renderTagsGroup(activeSideAFilter, litigationSideAOptions, 'sideAFilter')}
{this.renderTagsGroup(activeSideBFilter, litigationSideBOptions, 'sideBFilter')}
{this.renderTagsGroup(activeSideCFilter, litigationSideCOptions, 'sideCFilter')}
{this.renderTagsGroup(activeSectorsFilter, sectorsOptions, 'sectorsFilter')}
{this.renderTagsGroup(activePartyTypesFilter, litigationPartyTypesOptions, 'partyTypeFilter')}
{this.renderTagsGroup(activeJurisdictionsFilter, litigationJurisdictionsOptions, 'jurisdictionFilter')}
{this.renderTagsGroup(activeSideAPartyTypesFilter, litigationSideAPartyTypeOptions, 'sideAPartyTypeFilter')}
Expand Down Expand Up @@ -243,7 +251,8 @@ class LitigationCases extends Component {
litigation_side_c_names_options: litigationSideCOptions,
litigation_side_a_party_type_options: litigationSideAPartyTypeOptions,
litigation_side_b_party_type_options: litigationSideBPartyTypeOptions,
litigation_side_c_party_type_options: litigationSideCPartyTypeOptions
litigation_side_c_party_type_options: litigationSideCPartyTypeOptions,
sectors_options: sectorsOptions
} = this.props;

return (
Expand Down Expand Up @@ -283,6 +292,12 @@ class LitigationCases extends Component {
params={litigationPartyTypesOptions}
onChange={(event) => this.filterList('activePartyTypesFilter', event)}
/>
<SearchFilter
ref={this.sectorsFilter}
filterName="Sectors"
params={sectorsOptions}
onChange={(event) => this.filterList('activeSectorsFilter', event)}
/>
<SearchFilter
ref={this.sideAPartyTypeFilter}
filterName="Side A Type"
Expand Down Expand Up @@ -382,16 +397,16 @@ class LitigationCases extends Component {
<Fragment key={i}>
<li className="content-item">
<h5 className="title" dangerouslySetInnerHTML={{__html: litigation.link}} />
<div className="meta">
{litigation.geography && (
<a href={litigation.geography_path}>
<img src={`/images/flags/${litigation.geography.iso}.svg`} alt="" />
{litigation.geography.name}
</a>
)}
{litigation.opened_in && <div>Opened in {litigation.opened_in}</div>}
{litigation.last_development_in && <div>Last development in {litigation.last_development_in}</div>}
</div>
<div className="meta">
{litigation.geography && (
<a href={litigation.geography_path}>
<img src={`/images/flags/${litigation.geography.iso}.svg`} alt="" />
{litigation.geography.name}
</a>
)}
{litigation.opened_in && <div>Opened in {litigation.opened_in}</div>}
{litigation.last_development_in && <div>Last development in {litigation.last_development_in}</div>}
</div>
<div className="description" dangerouslySetInnerHTML={{__html: litigation.summary}} />
</li>
</Fragment>
Expand Down Expand Up @@ -427,7 +442,8 @@ LitigationCases.defaultProps = {
litigation_side_a_party_type_options: [],
litigation_side_b_party_type_options: [],
litigation_side_c_party_type_options: [],
litigation_jurisdictions_options: []
litigation_jurisdictions_options: [],
sectors_options: []
};

LitigationCases.propTypes = {
Expand All @@ -444,7 +460,8 @@ LitigationCases.propTypes = {
litigation_side_a_party_type_options: PropTypes.array,
litigation_side_b_party_type_options: PropTypes.array,
litigation_side_c_party_type_options: PropTypes.array,
litigation_jurisdictions_options: PropTypes.array
litigation_jurisdictions_options: PropTypes.array,
sectors_options: PropTypes.array
};

export default LitigationCases;
4 changes: 2 additions & 2 deletions app/models/laws_sector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class LawsSector < ApplicationRecord
belongs_to :parent, class_name: 'LawsSector', foreign_key: 'parent_id', optional: true
has_many :subsectors, class_name: 'LawsSector', foreign_key: 'parent_id'
has_many :targets, foreign_key: 'sector_id'
has_many :litigations, foreign_key: 'sector_id'
has_many :legislations, foreign_key: 'sector_id'
has_and_belongs_to_many :litigations
has_and_belongs_to_many :legislations

validates :name, presence: true, uniqueness: true
end
7 changes: 7 additions & 0 deletions app/services/queries/cclow/legislation_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def call(scope = Legislation.published)
.merge(filter_by_to_date)
.merge(filter_by_type)
.merge(filter_recent)
.merge(filter_by_sector)
.merge(order)
end

Expand All @@ -45,6 +46,12 @@ def filter_by_region
scope.includes(:geography).where(geographies: {region: params[:region]})
end

def filter_by_sector
return scope unless params[:law_sector].present?

scope.includes(:laws_sectors).where(laws_sectors: {id: params[:law_sector]})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as below

end

def filter_by_tags
sql = ''

Expand Down
7 changes: 7 additions & 0 deletions app/services/queries/cclow/litigation_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def call(scope = Litigation.published)
.merge(filter_by_litigation_side_party_types)
.merge(filter_by_litigation_party_type)
.merge(filter_by_jurisdiction)
.merge(filter_by_sector)
.merge(filter_recent)
.merge(order)
end
Expand All @@ -43,6 +44,12 @@ def filter_by_region
scope.includes(:geography).where(geographies: {region: params[:region]})
end

def filter_by_sector
return scope unless params[:law_sector].present?

scope.includes(:laws_sectors).where(laws_sectors: {id: params[:law_sector]})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can just use joins here, if we don't need to include the laws_sectors objects in Ruby.

end

def filter_by_geography
return scope unless params[:geography].present?

Expand Down