Thursday, March 27, 2014

Scopes -Sort Issue

The issue I am working on (still)
When the user sorts for say the products, most recent or whatever;
there is no real indicator that you are on the sorted "page."

Questions:

1. Can this be handled in the css?

No, you need helper methods.

I had tried adding a bunch of changes in the CSS to little avail. I am not a css expert but I think I fiddled around with it enough that I am coming to the conclusion that this is NOT how to solve this issue.
I did learn about first-child and last-child
http://www.w3schools.com/cssref/tryit.asp?filename=trycss_sel_firstchild
p:first-childd
{
background-color:yellow;
}

2. Is it actually a new page when you sort?

No, it using same page template but it is passing a variable into the application to alter the view.

When you sort for the product the url changes
from: http://example-co.webrand.dev:3000/crowdshops
to:     http://example-co.webrand.dev:3000/crowdshops/a-simple-crowdshop?scopes%5B%5D=popular

In the Gemfile there is the gem "jquery-rails", "~> 3.0"
I am watching a RailsCast on Jquery to see if this gives me a clue.
 http://railscasts.com/episodes/136-jquery

https://github.com/rails/jquery-ujs
 Interesting, Andre Arco from the Bundler core team is a contributer of the write of this gem. :)

I had looked at this article yesterday but it doesn't really answer my questions and is so advanced I'm not really sure what he is saying through most of the article. http://robots.thoughtbot.com/a-tour-of-rails-jquery-ujs

I think it might be a change in the javascript but I can't find a good tutorial about jquery-rails to help me know what to do.

Ashok asked me: Is scopes an array? What does ?scopes%5B%5D=popular mean
We put it in a url decoder: http://meyerweb.com/eric/tools/dencoder/
It returned: Ashok asked me: ?scopes[]=popular
? Are you adding the claases for sorting from the template or via Javascipt. ?


app/assets/javascripts/crowdshop.js
$(document).ready(function() {
  $("#order_confirmation_modal").modal();
  $("#submission_confirmation_modal").modal();
  $('.collapse').on('show.bs.collapse', function () {
   $('.panel-title i').removeClass( "fa-chevron-down" ).addClass( "fa-chevron-up" );
  })
  $('.collapse').on('hide.bs.collapse', function () {
   $('.panel-title i').removeClass( "fa-chevron-up" ).addClass( "fa-chevron-down" );
  })
})

  def with_dynamic_scoping(scope)
    if params[:scopes]
      scope.oscoping(params[:scopes], params[:scope])
    else
      scope
    end
  end

params is the url parameter ?scopes[]=popular

Railscast 228

app/controllers/crowdshops_controller.rb

  def with_dynamic_scoping(scope)
    if params[:scopes]
      scope.oscoping(params[:scopes], params[:scope])
    else
      scope
    end
  end

GOES WITH:

app/models/design.rb

  def self.oscoping(functions, arguments = {})
    functions.inject(self) do |query, function|
      if arguments && arguments[function]
        query.send(function, *arguments[function])
      else
        query.send(function)
      end
    end
  end



inject maps a function to run across all the elements or a subset of elements in the array

in this case it takes the params array

http://stackoverflow.com/questions/710501/need-a-simple-explanation-of-the-inject-method

what is .send ??
http://ruby-doc.org/core-1.9.3/Object.html#method-i-send

oscoping object scope?

  def self.oscoping(functions, arguments = {})
    functions.inject(self) do |query, function|
      if arguments && arguments[function]
        query.send(function, *arguments[function])
      else
        query.send(function)
      end
    end
  end

def self.whatever
http://www.jimmycuadra.com/posts/self-in-ruby

In the context of a class, self refers to the current class, which is simply an instance of the class Class. Defining a method on self creates a class method.

main difference def self.method is that you have to behavior within the scope of the variable
while def method is global

grepped for with_dynamic_scoping

app/controllers/crowdshops_controller.rb


  def with_dynamic_scoping(scope)
    if params[:scopes]
      scope.oscoping(params[:scopes], params[:scope])
    else
      scope
    end
  end

 def show
    @_designs = with_dynamic_scoping(@_crowdshop.designs.approved).includes(:account)

  end

grepped for @_crowdshop

scope is an instance of the design varible
params[:scopes] is an array variable

def methods for what page we are on and what parameters we are passing

<%= debug(params[:scopes]) %>
<&= inspect ??? google

content_tag 
http://apidock.com/rails/ActionView/Helpers/TagHelper/content_tag

No comments:

Post a Comment