Rails - routes and scopes

326 Views Asked by At

I am trying to make an app with Rails 4.

I have 4 relevant models for this question, being Project, Scope, Disclosure, Finalise.

The associations are:

Project.rb:

has_one :scope
accepts_nested_attributes_for :scope

In my project.rb model, I have tried to define scopes as:

 scope :visible, -> { joins(:disclosures).group("projects.id").merge(Disclosure.full_disclosure)}

  scope :finalised, -> { joins(:finalises).group("projects.id").merge(Finalise.published)}

Scope.rb

belongs_to :project
has_one :disclosure
has_one :finalise

accepts_nested_attributes_for :disclosure,  :finalise

Disclosure.rb:

  belongs_to :scope

Finalise.rb:

  belongs_to :scope #, counter_cache: true

In my projects controller, I have an action defined as:

  def all
    @projects = Project.visible.finalised
    render 'index'
  end

My aim is for that action to show only the projects which are marked as finalised and public (as those scopes are defined in the Finalise and Disclosure models.

In my disclosure.rb I have

      scope :full_disclosure, lambda {where('allusers' == true)}

I have an attribute in my disclosure table called :allures

In my finalise.rb, I have:

  scope :published, lambda {where('draft' == false )}

I have an attribute in my finalise table called :draft.

In my projects index view, I have:

<% @projects.in_groups_of(3) do |group| %>
    <div class="row">
      <% group.compact.each do |project| %>
          <div class="col-md-4">
            <div class="indexdisplay">
            <%= image_tag project.hero_image_url, width: '100%', height: '200px' if project.hero_image.present? %>
                <div class="indexheading"> <%= link_to project.title, project %> </div>
              <div class="indexsubtext">    <%= truncate(project.description, :ommission => "...", :length => 250) %></div>
               </div>
          </div>
            <% end %>
            </div>

      <% end %>

In my routes.rb, I have:

 resources :projects do
    get 'projects/all' => 'projects#all', :as => 'all_current_projects'
 end

The part I don't know is how do I make the view, pick up the project#all action?

When I try typing projects#all in my url, I get projects which are marked as drafts (so not meeting the scope I defined in my finalise.rb). I don't know if I haven't picked up the criteria properly in the scopes that I have tried to merge in the project.rb, and in any event, I don't know how to get my index view to work with this controller action.

I'm stuck. What I have written clearly doesn't work - (because drafts are included in the output when I type project#all in the browser; and then also because I don't know how to make the view work with the scopes that I have defined in the model and controller.

When I rake routes, I get:

project_all_current_projects GET      /projects/:project_id/projects/all(.:format)                                                    projects#all
                   project_available_students GET      /projects/:project_id/projects/currently_available_students(.:format)                           projects#currently_available_students
                   project_available_sponsors GET      /projects/:project_id/projects/currently_available_sponsors(.:format)                           projects#currently_available_sponsors
            project_expiring_student_projects GET      /projects/:project_id/projects/expiring_soon_students(.:format)                                 projects#expiring_soon_students
            project_expiring_sponsor_projects GET      /projects/:project_id/projects/expiring_soon_sponsors(.:format)                                 projects#expiring_soon_sponsors

I think the merge function might need something added to it to go from projects, through scope to finalise/disclosure.

0

There are 0 best solutions below