This commit is contained in:
Renzo 2016-08-14 14:52:08 -05:00
parent 48b182b2f6
commit b009bb77ad
2 changed files with 24 additions and 12 deletions

View File

@ -9,7 +9,7 @@ class TagSubscription extends Rails\ActiveRecord\Base
]
];
}
protected function callbacks()
{
return [
@ -21,7 +21,7 @@ class TagSubscription extends Rails\ActiveRecord\Base
]
];
}
protected function scopes()
{
return [
@ -30,7 +30,7 @@ class TagSubscription extends Rails\ActiveRecord\Base
}
];
}
public function normalize_name()
{
/**
@ -40,26 +40,26 @@ class TagSubscription extends Rails\ActiveRecord\Base
*/
$this->name = preg_replace(['/\P{L}/', '/_{2,}/'], '_', mb_convert_encoding($this->name, 'ISO-8859-2'));
}
public function initialize_post_ids()
{
if ($this->user->is_privileged_or_higher()) {
$this->cached_post_ids = join(',', array_unique(Post::find_by_tags($this->tag_query, ['limit' => ceil(CONFIG()->tag_subscription_post_limit / 3), 'select' => 'p.id', 'order' => 'p.id desc'])->getAttributes('id')));
}
}
static public function find_post_ids($user_id, $name = null, $limit = null)
{
if (!$limit) {
$limit = CONFIG()->tag_subscription_post_limit;
}
$post_ids = self::select('cached_post_ids')->where(['user_id' => $user_id]);
if ($name) {
$post_ids->where('name LIKE ?', $name . '%');
}
$post_ids = $post_ids->take();
$parsed_ids = [];
foreach ($post_ids as $subs) {
$ids = explode(',', $subs->cached_post_ids);
@ -69,15 +69,15 @@ class TagSubscription extends Rails\ActiveRecord\Base
$parsed_ids = array_merge($parsed_ids, $ids);
}
sort($parsed_ids);
return array_slice(array_reverse(array_unique($parsed_ids)), 0, $limit);
}
static public function find_posts($user_id, $name = null, $limit = null)
{
return Post::available()->where('id IN (?)', self::find_post_ids($user_id, $name, $limit))->order('id DESC')->take();
}
static public function process_all()
{
foreach (self::all() as $tag_subscription) {
@ -110,4 +110,16 @@ class TagSubscription extends Rails\ActiveRecord\Base
}
}
}
protected function validations()
{
return [
'name' => [
'length' => ['minimum' => 1]
],
'tag_query' => [
'length' => ['minimum' => 1]
],
];
}
}

View File

@ -1,7 +1,7 @@
<tr id="tag-subscription-row-<?= $this->tag_subscription->id ?>">
<td><input onclick="new Ajax.Request('<?= $this->urlFor(['tag_subscription#destroy', 'id' => $this->tag_subscription->id, 'format' => 'js']) ?>', {asynchronous:true, evalScripts:true});" type="button" value="<?= '-' ?>"></td>
<td><?= $this->textFieldTag("tag_subscription[".$this->tag_subscription->id."][name]", $this->tag_subscription->name, ['size' => 20]) ?></td>
<td><?= $this->textFieldTag("tag_subscription[".$this->tag_subscription->id."][tag_query]", $this->tag_subscription->tag_query, ['size' => 70]) ?></td>
<td><?= $this->textFieldTag("tag_subscription[".$this->tag_subscription->id."][name]", $this->tag_subscription->name, ['size' => 20, 'required']) ?></td>
<td><?= $this->textFieldTag("tag_subscription[".$this->tag_subscription->id."][tag_query]", $this->tag_subscription->tag_query, ['size' => 70, 'required']) ?></td>
<td>
<?= $this->selectTag("tag_subscription[".$this->tag_subscription->id."][is_visible_on_profile]", $this->optionsForSelect(["Visible" => 1, "Hidden" => 0], $this->tag_subscription->is_visible_on_profile)) ?>
</td>