public function createFile() { $inflector = \Rails::services()->get('inflector'); $this->migrationClassName = $inflector->camelize($this->fileName); $this->fileName = gmdate('YmdHis') . '_' . $this->fileName; parent::createFile(); }
public static function tableName() { $cn = str_replace('\\', '_', get_called_class()); $inf = \Rails::services()->get('inflector'); $tableName = $inf->underscore($inf->pluralize($cn)); return static::tableNamePrefix() . $tableName . static::tableNameSuffix(); }
/** * This method allows to "overloadingly" get attributes this way: * $model->parentId; instead of $model->parent_id. */ public static function properAttrName($name) { if (static::convertAttributeNames()) { $name = \Rails::services()->get('inflector')->underscore($name); } return $name; }
public function master_class() { if ($this->table_name == 'pools_posts') { $class_name = 'PoolPost'; } else { $class_name = Rails::services()->get('inflector')->classify($this->table_name); } return $class_name; }
private static function loadLocales() { $i18n = \Rails::services()->get('i18n'); $locale = $i18n->locale(); if ($locale == 'es') { $file = __DIR__ . '/locales/' . $locale . '.yml'; } else { $file = __DIR__ . '/locales/en.yml'; } $i18n->loadLocale($file); }
public function submit($value = null, array $options = []) { if (!$value) { $inflector = \Rails::services()->get('inflector'); $prettyClassName = $inflector->humanize($inflector->underscore(get_class($this->model))); if ($this->model->isNewRecord()) { $value = 'Create ' . $prettyClassName; } else { $value = 'Update ' . $prettyClassName; } } return $this->helper->tag('input', array_merge($options, ['value' => $value, 'type' => 'submit'])); }
public static function create_instance(Post $post) { $image_store = Rails::services()->get('inflector')->camelize(CONFIG()->image_store); $file = dirname(__FILE__) . '/' . $image_store . '.php'; if (!is_file($file)) { throw new Exception(sprintf("File not found for image store configuration '%s'.", CONFIG()->image_store ?: '[empty value]')); } require_once $file; $class = 'Post_ImageStore_' . $image_store; $object = new $class(); $object->_post = $post; return $object; }
public function build_query() { $params = $this->params; if (empty($params['foreign_key'])) { $inflector = \Rails::services()->get('inflector'); $cn = get_class($this->parent_model); $params['foreign_key'] = $inflector->singularize($cn::tableName()) . '_id'; } $query = new Relation($params['class_name'], $params['class_name']::tableName()); $query->where('`' . $params['foreign_key'] . "` = ?", $this->parent_model->id); # params[0], if present, it's an anonymous function to customize the relation. # The function is binded to the relation object. if (isset($this->params[0])) { $lambda = array_shift($this->params); $lambda = $lambda->bindTo($query); $lambda(); } $this->query = $query; }
public function group_by_table_class() { return Rails::services()->get('inflector')->classify($this->group_by_table); }
protected function getNamedPath($method, array $params = []) { $alias = \Rails::services()->get('inflector')->underscore(substr($method, 0, -4)); return \Rails::application()->router()->url_helpers()->find_route_with_alias($alias, $params); }
public function index() { $this->helper('Tag', 'Post'); $search = trim($this->params()->search) ?: ""; $q = ['keywords' => []]; if ($search) { foreach (explode(' ', $search) as $s) { if (preg_match('/^(.+?):(.*)/', $s, $m)) { $search_type = $m[1]; $param = $m[2]; if ($search_type == "user") { $q['user'] = $param; } elseif ($search_type == "change") { $q['change'] = (int) $param; } elseif ($search_type == "type") { $q['type'] = $param; } elseif ($search_type == "id") { $q['id'] = (int) $param; } elseif ($search_type == "field") { # 'type' must also be set for this to be used. $q['field'] = $param; } else { # pool'123' $q['type'] = $search_type; $q['id'] = (int) $param; } } else { $q['keywords'][] = $s; } } } $inflector = Rails::services()->get('inflector'); if (!empty($q['type'])) { $q['type'] = $inflector->pluralize($q['type']); } if (!empty($q['inner_type'])) { $q['inner_type'] = $inflector->pluralize($q['inner_type']); } # If notes'id' has been specified, search using the inner key in history_changes # rather than the grouping table in histories. We don't expose this in general. # Searching based on hc.table_name without specifying an ID is slow, and the # details here shouldn't be visible anyway. if (array_key_exists('type', $q) and array_key_exists('id', $q) and $q['type'] == "notes") { $q['inner_type'] = $q['type']; $q['remote_id'] = $q['id']; unset($q['type']); unset($q['id']); } $query = History::none(); $hc_conds = []; $hc_cond_params = []; if (!empty($q['user'])) { $user = User::where('name', $q['user'])->first(); if ($user) { $query->where("histories.user_id = ?", $user->id); } else { $query->where("false"); } } if (!empty($q['id'])) { $query->where("group_by_id = ?", $q['id']); } if (!empty($q['type'])) { $query->where("group_by_table = ?", $q['type']); } if (!empty($q['change'])) { $query->where("histories.id = ?", $q['change']); } if (!empty($q['inner_type'])) { $q['inner_type'] = $inflector->pluralize($q['inner_type']); $hc_conds[] = "hc.table_name = ?"; $hc_cond_params[] = $q['inner_type']; } if (!empty($q['remote_id'])) { $hc_conds[] = "hc.remote_id = ?"; $hc_cond_params[] = $q['remote_id']; } if ($q['keywords']) { $hc_conds[] = 'hc.value LIKE ?'; $hc_cond_params[] = '%' . implode('%', $q['keywords']) . '%'; } if (!empty($q['field']) and !empty($q['type'])) { # Look up a particular field change, eg. "type'posts' field'rating'". # XXX: The WHERE id IN (SELECT id...) used to implement this is slow when we don't have # anything } else { filtering the results. $field = $q['field']; $table = $q['type']; # For convenience: if ($field == "tags") { $field = "cached_tags"; } # Look up the named class. if (!Versioned::is_versioned_class($cls)) { $query->where("false"); } else { $hc_conds[] = "hc.column_name = ?"; $hc_cond_params[] = $field; # A changes that has no previous value is the initial value for that object. Don't show # these changes unless they're different from the default for that field. list($default_value, $has_default) = $cls::versioning()->get_versioned_default($field); if ($has_default) { $hc_conds[] = "(hc.previous_id IS NOT NULL OR value <> ?)"; $hc_cond_params[] = $default_value; } } } if ($hc_conds) { array_unshift($hc_cond_params, 'histories.id IN (SELECT history_id FROM history_changes hc JOIN histories h ON (hc.history_id = h.id) WHERE ' . implode(" AND ", $hc_conds) . ')'); call_user_func_array([$query, 'where'], $hc_cond_params); } if (!empty($q['type']) and empty($q['change'])) { $this->type = $q['type']; } else { $this->type = "all"; } # 'specific_history' => showing only one history # 'specific_table' => showing changes only for a particular table # 'show_all_tags' => don't omit post tags that didn't change $this->options = ['show_all_tags' => $this->params()->show_all_tags == "1", 'specific_object' => !empty($q['type']) and !empty($q['id']), 'specific_history' => !empty($q['change'])]; $this->options['show_name'] = false; if ($this->type != "all") { $cn = $inflector->classify($this->type); try { if (Versioned::is_versioned_class($cls) && class_exists($cn)) { $obj = new $cn(); if (method_exists($obj, "pretty_name")) { $this->options['show_name'] = true; } } } catch (Rails\Loader\Exception\ExceptionInterface $e) { } } $this->changes = $query->order("histories.id DESC")->select('*')->paginate($this->page_number(), 20); # If we're searching for a specific change, force the display to the # type of the change we found. if (!empty($q['change']) && $this->changes->any()) { $this->type = $inflector->pluralize($this->changes[0]->group_by_table); } $this->render(['action' => 'index']); }
/** * @param array $params - Additional parameters to customize the query for the association */ private function _find_has_many($prop, $params) { $inflector = \Rails::services()->get('inflector'); empty($params['class_name']) && ($params['class_name'] = $inflector->camelize($inflector->singularize($prop))); $builder = new Association($params, $this); $builder->build_query(); return $builder->get_query()->take() ?: false; }
public function versioned_master_object() { $parent = self::versioning()->get_versioned_parent(); if (!$parent) { return null; } $type = \Rails::services()->get('inflector')->classify($parent['class']); $foreign_key = $parent['foreign_key']; $id = $this->{$foreign_key}; return $type::find($id); }
public function versioned_parent($c, array $options = []) { if (isset($options['foreign_key'])) { $foreign_key = $options['foreign_key']; } else { $foreign_key = \Rails::services()->get('inflector')->underscore($c) . '_id'; } $this->versioned_parent = ['class' => $c, 'foreign_key' => $foreign_key]; return $this; }
private function createHelperFile($base_name) { $name = $base_name . '_helper.php'; $path = Rails::config()->paths->helpers; if (is_file($path . '/' . $name)) { return; } $class_name = Rails::services()->get('inflector')->camelize($base_name) . 'Helper'; $contents = "<?php\nclass {$class_name} extends Rails\\ActionView\\Helper\n{\n \n}"; file_put_contents($path . '/' . $name, $contents); }
public function getService($name) { return \Rails::services()->get($name); }
protected function runCallbackKind($name, $kind, array $callbacks) { # TODO: services $inflector = \Rails::services()->get('inflector'); $key = $inflector->camelize($kind . '_' . $name, false); if (isset($callbacks[$key])) { foreach ($callbacks[$key] as $methodName => $options) { $ret = $this->{$methodName}(); if ($kind == 'before' && $ret === false) { return false; } } } return true; }
public function inflector() { return \Rails::services()->get('inflector'); }