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

Added permissions to badges #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Added permissions to badges #36

wants to merge 3 commits into from

Conversation

bruncbrunc
Copy link
Contributor

No description provided.

@@ -70,7 +70,7 @@ def self.filter (user, title, content, reply, label, author, query, forum)
order_phrase = query || [title, content, reply].select(&:present?).join(" ")
user_id = user.try(:id).to_i
role_value = user.try(:role).to_i
can_read = "COALESCE(forum_role_read.value, 0) <= ? AND COALESCE(forumgroup_role_read.value, 0) <= ?"
can_read = "(COALESCE(forum_role_read.value, 0) <= ? AND COALESCE(forumgroup_role_read.value, 0) <= ?)"
Copy link
Contributor

Choose a reason for hiding this comment

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

whar are these for?

db/schema.rb Outdated
t.string "title", limit: 255
t.text "content", limit: 16777215
t.string "title", limit: 191
t.text "content", limit: 65535
Copy link
Contributor

Choose a reason for hiding this comment

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

please don't shorten any text limits

Copy link
Contributor

@jomo jomo left a comment

Choose a reason for hiding this comment

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

You can throw away half of your code and just use collection_check_boxes 😎

Other than that, if I'm not mistaken you should be able to use something like

@forum.badges.create!(badge: b, permission: p)

and

@forum.badges.find_by(id: b, permission: p)

Also, you can use

Badge.where.not(name: "none")

<td><b>Badges with read permission</b></td>
<td>
<% Badge.where("name != 'none'").each do |b| %>
<%=b%><%= check_box_tag "read-#{b}", nil, Badgeassociation.find_by(badge: b, forumgroup: @group, permission: 1) %>
Copy link
Contributor

Choose a reason for hiding this comment

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

You could use f.check_box instead, this would also generate a proper label tag.

@@ -86,7 +86,7 @@ def self.filter (user, title, content, reply, label, author, query, forum)
.joins("LEFT JOIN roles as forumgroup_role_read ON forumgroups.role_read_id = forumgroup_role_read.id")
.joins("LEFT JOIN roles as forumgroup_role_write ON forumgroups.role_write_id = forumgroup_role_write.id")

threads = threads.where("forumthreads.user_author_id = ? OR (#{can_read}) OR (#{sticky_can_write})", user_id, role_value, role_value, role_value, role_value)
threads = threads.where("forumthreads.user_author_id = ? OR (#{can_read}) OR (#{sticky_can_write}) OR (?)", user_id, role_value, role_value, role_value, role_value, Forum.find(forum).can_read?(user))
Copy link
Contributor

Choose a reason for hiding this comment

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

Couldn't you just use forum.can_read?

Copy link
Contributor

Choose a reason for hiding this comment

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

Since this results in:

  • OR (true) (which will always match) or
  • OR (false) (which will have no effect on the current query)

you could just wrap the query in a condition and not filter them any further.
Also, given that (#{can_read}) just mimics forum.can_read? in the SQL query, you don't need to include it anymore:

unless forum.can_read?(user)
  threads = threads.where("forumthreads.user_author_id = ? OR (#{sticky_can_write})", user_id, role_value, role_value)
end

@bruncbrunc
Copy link
Contributor Author

where.not, if I'm not mistaken, was added in rails 5. We are using rails 4.

@jomo
Copy link
Contributor

jomo commented Aug 6, 2017

Loading development environment (Rails 4.2.5.1)
irb(main):001:0> Badge.where.not(name: :none)
  Badge Load (0.4ms)  SELECT `badges`.* FROM `badges` WHERE (`badges`.`name` != 'none')
┌────┬───────────┬────────┬───────┬───────┐
│ id │ name      │ symbol │ color │ value │
├────┼───────────┼────────┼───────┼───────┤
│ 2  ╎ donor     ╎ $      ╎ #f60  ╎       │
│ 3  ╎ developer ╎ D      ╎ #a0a  ╎       │
│ 4  ╎ retired   ╎ R      ╎ #0aa  ╎       │
│ 5  ╎ lead      ╎ L      ╎ #a00  ╎       │
└────┴───────────┴────────┴───────┴───────┘
4 rows in set

@bruncbrunc
Copy link
Contributor Author

In that case I have no clue, but I definitely wasn't able to do that when I was a developer.

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

Successfully merging this pull request may close these issues.

3 participants