Skip to content

Instantly share code, notes, and snippets.

@secretpray
Last active February 28, 2024 16:49
Show Gist options
  • Save secretpray/cbbb2bb6d4a1a8261ec9fec4661d0b32 to your computer and use it in GitHub Desktop.
Save secretpray/cbbb2bb6d4a1a8261ec9fec4661d0b32 to your computer and use it in GitHub Desktop.
New tally method (Ruby 2.7)
def search_result
  categories.each_with_object([]) do |category, keywords|
    keywords << category.keywords.split(', ').select{ |c| c.include?(query) } unless category.keywords.blank?
  end.flatten.tally.first(3).map(&:first)
end
[1, 2, 2, 3].tally
# => { 1 => 1, 2 => 2, 3 => 1 }
%w(foo foo bar foo baz foo).tally
=> {"foo"=>4, "bar"=>1, "baz"=>1}
%w(foo foo bar foo baz foo).map { |s| s[0] }.tally
=> {“f” => 4, “b” => 2}

There’s discussion happening at the moment on accepting this feature, which would make the above syntax:

%w(foo foo bar foo baz foo).tally_by { |s| s[0] }
=> {“f” => 4, “b” => 2}

alternative

list.group_by { |v| v.something }.transform_values(&:size)
list.group_by { |v| v.something }.map { |k, vs| [k, vs.size] }.to_h
list.group_by { |v| v.something }.to_h { |k, vs| [k, vs.size] }
list.each_with_object(Hash.new(0)) { |v, h| h[v.something] += 1 }
@secretpray
Copy link
Author

@poll.poll_options.sum(:votes)

Then to create the % for the PHP for example you could do

total_votes = @poll.poll_options.sum(:votes)
php_votes = @poll.poll_options.where(name: 'PHP').votes
(php_votes / total_votes).to_i * 100

@secretpray
Copy link
Author

# app/controllers/concerns/voting_controller.rb
def obtain_resources
  model = controller_name.singularize.camelize.constantize
  instance_name = controller_name.singularize
  # here you get @universe or @character
  instance_variable_set "@#{instance_name}", model.find(params[:id])
end

@secretpray
Copy link
Author

secretpray commented Dec 21, 2022

def obtain_resources
  redirect_back(fallback_location: root_path) unless params[:id]

  klass_name, id = params[:id].split('_') # -> ['post', '12']
  redirect_back(fallback_location: root_path) unless %w[product service post].include?(klass_name)

  klass_name.classify.constantize.find_by(id: id) # -> Post.find_by(id: id)
end
<% ident_object = dom_id(object) %>
<% if user_signed_in? && object.favorites.exists?(owner: user) %>
  <%= link_to favorites_path(id: ident_object), method: :delete,
              class: 'favorites-link',
              data: { action: 'click->favorites#clearFromList',
                      ident_object: ident_object
                    } do %>
    <i class="fas fa-heart opacity-75"></i>
  <% end %>
<% else %>
  <%= link_to favorites_path(id: ident_object), method: :post,
              class: 'favorites-link' do %>
  <i i class="far fa-heart"></i>
  <% end %>
<% end %>

@secretpray
Copy link
Author

secretpray commented Dec 24, 2022

Service.all.pluck(:owner_id).tally
Service.group(:owner_id).size
Service.pluck(:owner_id).tally.sort_by{_2}.to_h
Service.pluck(:owner_id).tally.sort_by{-_2}.to_h
Service.group(:owner_id).size.sort_by(&:last).to_h
Service.group(:owner_id).size.sort_by(&:last).reverse.to_h
Service.group(:owner_id).size.sort_by{ |_, v| -v}.to_h
Service.group(:owner_id).size.sort_by{ |_, v| v}.reverse.to_h
Service.group(:owner_id).size.sort_by{_2}.to_h
Service.group(:owner_id).size.sort_by{_2}.reverse.to_h
Service.group(:owner_id).size.sort_by{-_2}.to_h

@secretpray
Copy link
Author

PS Search with Sphinx

class SearchService
  SEARCH_TYPES = %w[all question answer comment user].freeze

  def self.call(params)
    new(params).search
  end

  def initialize(params)
    @search_query = params[:search_query]
    @search_by    = params[:search_by]
  end

  def search
    return [] if @search_query.blank? || !SEARCH_TYPES.include?(@search_by)

    if @search_by == 'all'
      ThinkingSphinx.search(@search_query)
    else
      Object.const_get(@search_by.capitalize).search(@search_query)
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment